1、MainActivity package cn.dccssq; import android.app.ListActivity; import android.content.Intent; import android.database.Cursor; import android.os.Bundle; import android.util.Log; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.ListAdapter; import android.widget.ListView; import android.widget.SimpleCursorAdapter; public class MainActivity extends ListActivity { private static final int INSERT_ID = Menu.FIRST; private static final int DELETE_ID = Menu.FIRST + 1; private static final int ACTIVITY_CREATE = 0; private static final int ACTIVITY_EDIT = 1; private DiaryDbAdapter diaryDb; private Cursor cursor; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); diaryDb =new DiaryDbAdapter(this); diaryDb.open(); } private void showListView(){ cursor = diaryDb.getAllNotes(); String[] from = new String[]{DiaryDbAdapter.KEY_TITLE,DiaryDbAdapter.KEY_BODY}; int[] to = new int[]{R.id.text1,R.id.created}; ListAdapter cursorAdapter = new SimpleCursorAdapter(this,R.layout.diary_row,cursor,from,to); setListAdapter(cursorAdapter); } @Override protected void onListItemClick(ListView l, View v, int position, long id) { // TODO Auto-generated method stub super.onListItemClick(l, v, position, id); Cursor c = cursor; c.move(position); Intent intent = new Intent(this,ActivityDiary.class); intent.putExtra(DiaryDbAdapter.KEY_ROWID, id); intent.putExtra(DiaryDbAdapter.KEY_TITLE, c.getString(c .getColumnIndexOrThrow(DiaryDbAdapter.KEY_TITLE))); intent.putExtra(DiaryDbAdapter.KEY_BODY, c.getString(c .getColumnIndexOrThrow(DiaryDbAdapter.KEY_BODY))); startActivityForResult(intent, ACTIVITY_EDIT); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { // TODO Auto-generated method stub super.onActivityResult(requestCode, resultCode, data); showListView(); } @Override public boolean onCreateOptionsMenu(Menu menu) { // TODO Auto-generated method stub super.onCreateOptionsMenu(menu); menu.add(0,INSERT_ID,0,R.string.menu_insert); menu.add(0,DELETE_ID,0,R.string.menu_delete); return true; } @Override public boolean onMenuItemSelected(int featureId, MenuItem item) { // TODO Auto-generated method stub switch(item.getItemId()){ case INSERT_ID: Log.i("INSERT:", String.valueOf(INSERT_ID)); createDiary(); return true; case DELETE_ID: Log.i("DELETE_ID:", String.valueOf(getListView().getSelectedItemId())); diaryDb.deleteDiary(getListView().getSelectedItemId()); showListView(); return true; } return super.onMenuItemSelected(featureId, item); } private void createDiary(){ Intent intent = new Intent(); intent.setClass(this, ActivityDiary.class); startActivityForResult(intent, ACTIVITY_CREATE); } } 2、ActivityDiary package cn.dccssq; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.EditText; public class ActivityDiary extends Activity { // title EditText EditText titleTxt; // body Edi