使用Gmail账号登录 | 登录 | 注册我的空间

倒骑的驴的空间

首贴》android初探:NotePad Tutorial

发表于 2010年01月11日 22:45 分类: 工作日志 统计: 0评/166阅 0人关注此文章, 关注此文章(?)

 

这几天想着换工作,试着学习了下Android平台,下面是学习Android的一些心得。

 

 

声明:本人纯属菜鸟,大学专业是电子信息工程,毕业进了一个做系统集成的软件公司,由于志趣不投,想着换工作,于是自学起Android平台,想涉足移动应用开发领域。可能所言不得要领,希望大家海涵,更欢迎提意见者,以求共同进步。

1、Project Framework:

 
ProjectName      
  src Filter   stored java file
  gen Filter R.jave  generates auto,connot be modified.
R.java is the project's resource ID file,
these resources can be used in the *.java or *.xml file.
the resource in this file are coded by a unique number which is a '0x' num.
  res Filter drawable Filter Stored images file
    layout Filter Stored layout file,which are *.xml file and used to display
    values Filter Stored some values,such as 'string' values,which are used in program
  AndroidManifest.xml   The config file for the project,it is the kernel of the project
  default.properties   Also a config file,but I don't know what is its usage now

 

2、NotePad2的代码结构

             大致可以按照MVC模型来走,layoutèVIEWActivity.javaèCONTROLERDbAdptherèMODEL,但是各层之间的联系并未完全的被封装,只是提供了大量地类和方法实现各层之间的联系,例如NotePad例子中的SimpleCursorAdapter()(将DB中取出的Cursor映射到VIEW中的note_row,这一映射是在Activity中实现的)。

  

3、    其他几个重要的类:

Intent、用于Activity之间的数据传递,关系映射 

        Intent     An Intent provides a facility for performing late runtime binding between the code in different applications. Its most significant use is in the launching of activities, where it can be thought of as the glue between activities. It is basically a passive data structure holding an abstract description of an action to be performed.

       The primary pieces of information in an intent are: ActionData 

一种数据结构,用于Action之间的数据传递,相当于一个bean使用。 

除了可以携带数据外,还可以在两个Acation之间建立起其他的联系 

 

Bundle、封装数据,我认为当作bean来使用,或者可以看做是一个MapAndroid已将其封装好了。 

 

4、   About DB

SQLiteDatabase has methods to create, delete, execute SQL commands, and perform other common database management  tasks.

使用时只需要传入参数就行 

DB的链接使用open()方法,没有PC项目那么复杂,可能每个手机上都有自己独用的SQLiteDatabase ,所以用户名和密码什么的默认的或者是省略掉了。 

 

    但是好像没有在代码中发现关闭数据库连接的地方。

5、  Android的命名规则:

      、系统提供的方法on___形式,并且命名很全,一般能从名称中知道这个方法的作用

       、类成员变量均以m***大头打头,普通变量同Java规则。

6、NotePad3比较于NotePad2use life-cycle event callbacks to store and retrieve application state data 使得两个Activity. Notepadv2 && . NoteEdit)之间的耦合减小,.Notepadv2 使用startActivityForResult传递一个Intent到.NoteEdit,之后有关note_edit.xml页面的操作都交给了Notepadv2 自己

 

QMune中添加了一个Search选项,页面显示都很正常,但是当使用Title字段在DB中进行搜索并返回Cursor时出错,具体什么错误也没有报出来。

NotePad2.java

 public void fillData(String title) {
    	mNotesCursor = mDbHelper.searchNote(title);
        startManagingCursor(mNotesCursor);
        
        // Create an array to specify the fields we want to display in the list (only TITLE)
        String[] from = new String[]{NotesDbAdapter.KEY_TITLE};
        
        // and an array of the fields we want to bind those fields to (in this case just text1)
        int[] to = new int[]{R.id.text1};
        
        // Now create a simple cursor adapter and set it to display
        SimpleCursorAdapter notes = 
        	    new SimpleCursorAdapter(this, R.layout.notes_row, mNotesCursor, from, to);
        setListAdapter(notes);

 

NoteSearch.java

 

public class NoteSearch extends Activity {
	
	private EditText mTitleText;
	//private NotesDbAdapter mDbHelper;
	//private String mTitleTextStr;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.note_search);
		mTitleText = (EditText) findViewById(R.id.title);
		//mTitleTextStr = mTitleText.getText().toString();
		Button confirmButton = (Button) findViewById(R.id.confirm);
		//mDbHelper.open();
		
		confirmButton.setOnClickListener(new OnClickListener() {
			public void onClick(View view) {
				Bundle bundle = new Bundle();
		        
				//Cursor cursor = mDbHelper.searchNote(mTitleTextStr);
bundle.putSerializable(NotesDbAdapter.KEY_TITLE, mTitleText.getText().toString());
		    	
		    	Intent mIntent = new Intent();
		    	mIntent.putExtras(bundle);
		    	setResult(RESULT_OK, mIntent);
		    	finish();
			}
		});
	}
}

NotesDbAdapter.java 

 

    public Cursor searchNote(String title) {
    	Cursor mCursor = 
    		mDb.query(true, DATABASE_TABLE, new String[] {KEY_ROWID,
                    KEY_TITLE, KEY_BODY}, KEY_TITLE + "=" + title, 
                    null, null, null, null, null);
    	if (mCursor != null) {
            mCursor.moveToFirst();
        }
    	System.out.println("SSS");
    	return mCursor;
    }
共有0条网友评论

尚无网友评论

帐号:   密码:注册

只支持文本信息,最多不能超过250个字
最新网友评论

目前还没有任何评论

© 开源中国社区(OsChina.NET) | 关于我们 | 广告联系 | 站长空间 | 友情链接 | 开源中国手机版 | 粤ICP备08124133号