安卓记事本程序源代码
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
1、MainActivity
01.package cn.dccssq;
02.
03.import android.app.ListActivity;
04.import android.content.Intent;
05.import android.database.Cursor;
06.import android.os.Bundle;
07.import android.util.Log;
08.import android.view.Menu;
09.import android.view.MenuItem;
10.import android.view.View;
11.import android.widget.ListAdapter;
12.import android.widget.ListView;
13.import android.widget.SimpleCursorAdapter;
14.
15.public class MainActivity extends ListActivity {
16.
17. private static final int INSERT_ID = Menu.FIRST;
18.
19. private static final int DELETE_ID = Menu.FIRST + 1;
20.
21. private static final int ACTIVITY_CREATE = 0;
22.
23. private static final int ACTIVITY_EDIT = 1;
24.
25. private DiaryDbAdapter diaryDb;
26.
27. private Cursor cursor;
28. /** Called when the activity is first created. */
29. @Override
30. public void onCreate(Bundle savedInstanceState) {
31. super.onCreate(savedInstanceState);
32. setContentView(yout.main);
33.
34. diaryDb =new DiaryDbAdapter(this);
35. diaryDb.open();
36.
37. }
38.
39. private void showListView(){
40. cursor = diaryDb.getAllNotes();
41.
42. String[] from = new String[]{DiaryDbAdapter.KEY_TITLE,DiaryDbAdapter.KEY_BODY};
43. int[] to = new int[]{R.id.text1,R.id.created};
44.
45. ListAdapter cursorAdapter = new SimpleCursorAdapter(this,yout.diary_row,cursor,from,to);
46. setListAdapter(cursorAdapter);
47. }
48.
49. @Override
50. protected void onListItemClick(ListView l, View v, int position, long id) {
51. // TODO Auto-generated method stub
52. super.onListItemClick(l, v, position, id);
53. Cursor c = cursor;
54. c.move(position);
55. Intent intent = new Intent(this,ActivityDiary.class);
56. intent.putExtra(DiaryDbAdapter.KEY_ROWID, id);
57. intent.putExtra(DiaryDbAdapter.KEY_TITLE, c.getString(c
58. .getColumnIndexOrThrow(DiaryDbAdapter.KEY_TITLE)));
59. intent.putExtra(DiaryDbAdapter.KEY_BODY, c.getString(c
60. .getColumnIndexOrThrow(DiaryDbAdapter.KEY_BODY)));
61. startActivityForResult(intent, ACTIVITY_EDIT);
62. }
63.
64. @Override
65. protected void onActivityResult(int requestCode, int resultCode, Intent data) {
66. // TODO Auto-generated method stub
67. super.onActivityResult(requestCode, resultCode, data);
68. showListView();
69. }
70.
71. @Override
72. public boolean onCreateOptionsMenu(Menu menu) {
73. // TODO Auto-generated method stub
74. super.onCreateOptionsMenu(menu);
75. menu.add(0,INSERT_ID,0,R.string.menu_insert);
76. menu.a
dd(0,DELETE_ID,0,R.string.menu_delete);