1) Determine the data structure of the database 2) The icon to be used to copy the program into the RES/DRAWABLE-MDPI directory 3) Define string resource String.xml 4) Development layout file Activity_main.xml to display the contact list 5) Create a new detail.xml in the layout directory to display contact details 6) Develop database auxiliary class Myopenhelper class, create a new Myopenhelper.java 7) The next step into the development of the mainactivity end, to achieve database additions, deletions, change records and other operations 8) Create a new activity name called Detailactivity.java, realize the contact details display function |
Code: Mainactivity part of the code: Public classMainactivityextendsActivity {ListView list,lv; Myopenhelper DBHelper; Sqlitedatabase DB; String s; Button delbtn,addbtn; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.activity_main); LV=(ListView) Findviewbyid (R.ID.LISTVIEW1); ADDBTN=(Button) Findviewbyid (R.id.button1); DELBTN=(Button) Findviewbyid (R.id.button2); DBHelper=NewMyopenhelper (mainactivity. This, "Personal_contacts.db",NULL, 1); DB=dbhelper.getreadabledatabase (); ///Query Databasecursor cursor = db.rawquery ("SELECT * from Contacts",NULL); LOG.I ("Sssssssss", "111111"); Inflatelist (cursor); //Long Press Show DetailsLv.setonitemlongclicklistener (NewOnitemlongclicklistener () {@Override Public BooleanOnitemlongclick (adapterview<?> arg0, view view,intArg2,LongArg3) { //TODO auto-generated Method StubTextView TextView =(TextView) Findviewbyid (R.ID.TEXTVIEW1); S=Textview.gettext (). toString (); Intent Intent=NewIntent (mainactivity. This, Detailactivity.class); Bundle Bundle=NewBundle (); Bundle.putstring ("Name", s); Intent.putextras (bundle); StartActivity (Intent); Finish (); return false; } }); Delbtn.setonclicklistener (NewOnclicklistener () {@Override Public voidOnClick (View arg0) {//TODO auto-generated Method StubDb.delete ("Contacts", "Name=?",Newstring[]{s}); Toast.maketext (mainactivity. This, "Delete succeeded", Toast.length_long). Show (); Cursor Cursor=db.rawquery ("SELECT * from Contacts",NULL); Inflatelist (cursor); } }); Lv.setonitemclicklistener (NewOnitemclicklistener () {@Override Public voidOnitemclick (adapterview<?> arg0, view view,intArg2,LongArg3) { //TODO auto-generated Method StubView.setbackgroundcolor (Color.Blue); TextView TextView=(TextView) Findviewbyid (R.ID.TEXTVIEW1); S=Textview.gettext (). toString (); } }); Addbtn.setonclicklistener (NewOnclicklistener () {@Override Public voidOnClick (View arg0) {//TODO auto-generated Method StubIntent i=NewIntent (); I.setclass (mainactivity. This, Detailactivity.class); StartActivity (i); Finish (); } }); }Private voidinflatelist (cursor cursor) {LOG.I ("AAAAA", "111111"); intCount=Cursor.getcount (); String[] Name=NewString[count]; String[] Mobile=NewString[count]; intA=0; while(Cursor.movetonext ()) {Name[a]=cursor.getstring (Cursor.getcolumnindex ("name")); Mobile[a]=cursor.getstring (Cursor.getcolumnindex ("mobile")); A++; }arraylistNewArraylist(); LOG.I ("Sssssssss", "2222222"); for(inti=0;i<count;i++) {log.i ("QQQQQQQQQ", "111111"); HashMap<String,Object> map =NewHashmap<string, object>(); Map.put ("ItemText1", Name[i]); Map.put ("ItemText2", Mobile[i]); Listitem.add (map); } simpleadapter Listitemadater=NewSimpleadapter (mainactivity. This, Listitem,r.layout.item,Newstring[]{"ItemText1", "ItemText2"},New int[]{r.id.textview1,r.id.textview2}]; LOG.I ("JJJJJJJJJJJJJJJJ", "111111"); Lv.setadapter (Listitemadater); LOG.I ("Zzzzzz", "111111");} @Override Public BooleanOncreateoptionsmenu (Menu menu) {//inflate the menu; This adds items to the action bar if it is present.getmenuinflater (). Inflate (R.menu.main, menu); return true; }} Run Result: () |
This experiment feels very difficult, although initially has the general logic idea, in the process runs the program to crash, later examines for a long time only then discovers is because did not realize Findviewbyid this method, has no method compiles to the related code, solves this question, The program crashed when the new contact was saved, and the check found that there were two databases because the Myopenhelper.java database name was inconsistent with the Mainactivity.java database name. So, be careful when you experiment. |