首贴》android初探:NotePad Tutorial
这几天想着换工作,试着学习了下Android平台,下面是学习Android的一些心得。
声明:本人纯属菜鸟,大学专业是电子信息工程,毕业进了一个做系统集成的软件公司,由于志趣不投,想着换工作,于是自学起Android平台,想涉足移动应用开发领域。可能所言不得要领,希望大家海涵,更欢迎提意见者,以求共同进步。 1、Project Framework:
2、NotePad2的代码结构 大致可以按照MVC模型来走,layoutèVIEW;Activity.javaèCONTROLER;DbAdptherè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: Action、Data 一种数据结构,用于Action之间的数据传递,相当于一个bean使用。 除了可以携带数据外,还可以在两个Acation之间建立起其他的联系
Bundle、封装数据,我认为当作bean来使用,或者可以看做是一个Map,Android已将其封装好了。
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比较于NotePad2,use life-cycle event callbacks to store and retrieve application state data 使得两个Activity(. Notepadv2 && . NoteEdit)之间的耦合减小,.Notepadv2 使用startActivityForResult传递一个Intent到.NoteEdit,之后有关note_edit.xml页面的操作都交给了Notepadv2 自己。
Q:Mune中添加了一个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条网友评论
尚无网友评论 |
目前还没有任何评论