1¡¢MainActivity01.packagecn.dccssq;02.03.importandroid.app.ListActivity;04.importandroid.content.Intent;05.importandroid.database.Cursor;06.importandroid.os.Bundle;07.importandroid.util.Log;08.importandroid.view.Menu;09.importandroid.view.MenuItem;10.importandroid.view.View;11.importandroid.widget.ListAdapter;12.importandroid.widget.ListView;13.importandroid.widget.SimpleCursorAdapter;14.15.publicclassMainActivityextendsListActivity{16.17.privatestaticfinalintINSERT_ID=Menu.FIRST;18.19.privatestaticfinalintDELETE_ID=Menu.FIRST+1;20.21.privatestaticfinalintACTIVITY_CREATE=0;22.23.privatestaticfinalintACTIVITY_EDIT=1;24.25.privateDiaryDbAdapterdiaryDb;26.27.privateCursorcursor;28./**Calledwhentheactivityisfirstcreated.*/29.@Override30.publicvoidonCreate(BundlesavedInstanceState){31.super.onCreate(savedInstanceState);32.setContentView(R.layout.main);33.34.diaryDb=newDiaryDbAdapter(this);35.diaryDb.open();36.37.}38.39.privatevoidshowListView(){40.cursor=diaryDb.getAllNotes();41.42.String[]from=newString[]{DiaryDbAdapter.KEY_TITLE,DiaryDbAdapter.KEY_BODY};43.int[]to=newint[]{R.id.text1,R.id.created};44.45.ListAdaptercursorAdapter=newSimpleCursorAdapter(this,R.layout.diary_row,cursor,from,to);46.setListAdapter(cursorAdapter);47.}48.49.@Override50.protectedvoidonListItemClick(ListViewl,Viewv,intposition,longid){51.//TODOAuto-generatedmethodstub52.super.onListItemClick(l,v,position,id);53.Cursorc=cursor;54.c.move(position);55.Intentintent=newIntent(this,ActivityDiary.class);56.intent.putExtra(DiaryDbAdapter.KEY_ROWID,id);57.intent.putExtra(DiaryDbAdapter.KEY_TITLE,c.getString(c58..getColumnIndexOrThrow(DiaryDbAdapter.KEY_TITLE)));59.intent.putExtra(DiaryDbAdapter.KEY_BODY,c.getString(c60..getColumnIndexOrThrow(DiaryDbAdapter.KEY_BODY)));61.startActivityForResult(intent,ACTIVITY_EDIT);62.}63.64.@Override65.protectedvoidonActivityResult(intrequestCode,intresultCode,Intentdata){66.//TODOAuto-generatedmethodstub67.super.onActivityResult(requestCode,resultCode,data);68.showListView();69.}70.71.@Override72.publicbooleanonCreateOptionsMenu(Menumenu){73.//TODOAuto-generatedmethodstub74.super.onCreateOptionsMenu(menu);75.menu.add(0,INSERT_ID,0,R.string.menu_insert);76.menu.add(0,DELETE_ID,0,R.string.menu_delete);77.returntrue;78.}79.80.@Override81.publicbooleanonMenuItemSelected(intfeatureId,MenuItemitem){82.//TODOAuto-generatedmethodstub83.switch(item.getItemId()){84.caseINSERT_ID:85.Log.i(INSERT:,String.valueOf(INSERT_ID));86.createDiary();87.returntrue;88.caseDELETE_ID:89.Log.i(DELETE_ID:,String.valueOf(getListView().getSelectedItemId()));90.diaryDb.deleteDiary(getListView().getSelectedItemId());91.showListView();92.returntrue;93.}94.returnsuper.onMenuItemSelected(featureId,item);95.}96.97.privatevoidcreateDiary(){98.99.Intentintent=newIntent();100.intent.setClass(this,ActivityDiary.class);101.startActivityForResult(intent,ACTIVITY_CREATE);102.}103.}2¡¢ActivityDiary01.packagecn.dccssq;02.03.importandroid.app.Activity;04.importandroid.content.Intent;05.importandroid.os.Bundle;06.importandroid.util.Log;07.importandroid.view.View;08.importandroid.widget.Button;09.importandroid.widget.EditText;10.11.publicclassActivityDiaryextendsActivity{12.13.//titleEditText14.EditTexttitleTxt;15.//bodyEditText16.EditTextbodyTxt;17.//saveButton18.Buttonbtn;19.//RowId20.LongrowId;21.privateDiaryDbAdapterdiaryDb;22.23.@Override24.protectedvoidonCreate(BundlesavedInstanceState){25.//TODOAuto-generatedmethodstub26.super.onCreate(savedInstanceState);27.setContentView(R.layout.notepad);28.29.//InitializetheDiaryDbAdapter.30.diaryDb=newDiaryDbAdapter(this);31.32.//Getthescreencontrol33.titleTxt=(EditText)findViewById(R.id.title);34.bodyTxt=(EditText)findViewById(R.id.body_text);35.btn=(Button)findViewById(R.id.button);36.rowId=null;37.38.//Getdatafromthefrontpage39.Bundlebundle=getIntent().getExtras();40.41.if(bundle!=null){42.Log.i(bund:,bundle.toString());43.44.//Setdatatopage45.Stringtitle=bundle.getString(DiaryDbAdapter.KEY_TITLE);46.Stringbody=bundle.getString(DiaryDbAdapter.KEY_BODY);47.rowId=bundle.getLong(DiaryDbAdapter.KEY_ROWID);48.if(title!=null)49.{50.titleTxt.setText(title);51.}52.if(body!=null)53.{54.bodyTxt.setText(body);55.}56.}57.58.btn.setOnClickListener(newView.OnClickListener(){59.60.publicvoidonClick(Viewarg0){61.//TODOAuto-generatedmethodstub62.Stringtitle=titleTxt.getText().toString();63.Stringbody=bodyTxt.getText().toString();64.if(checkInput(title,body)){65.diaryDb.open();66.if(rowId!=null){67.diaryDb.updateDiary(rowId,title,body);68.}else{69.diaryDb.createDiary(title,body);70.}71.diaryDb.close();72.73.IntentmIntent=newIntent();74.setResult(RESULT_OK,mIntent);75.76.finish();77.}78.}79.});80.}81.82./**83.*Validatetheinput.84.*@paramtitle85.*@parambody86.*@return87.*/88.publicbooleancheckInput(Stringtitle,Stringbody){89.90.if(null==title||title.trim().length()==0){91.titleTxt.setError(Pleaseinputthetitle!);92.returnfalse;93.}94.95.if(null==body||body.trim().length()==0){96.bodyTxt.setError(Pleaseinputthecontent!);97.returnf