Code: mainactivity Code: public class Mainactivity extends Activity { Private ListView LV; protected void OnCreate (Bundle savedinstancestate) { Super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main); lv= (ListView) Findviewbyid (r.id.lv); Lv.setadapter (New arrayadapter<string> (Mainactivity.this, Android. R.layout.simple_list_item_single_choice)); Lv.setchoicemode (Listview.choice_mode_single); Final arraylist for (int i = 0; i < 3; i++) { Hashmap<string, object> map=new hashmap<string, object> (); Map.put ("Detailname", R.ID.EDITTEXT1); Map.put ("Detailmobile", r.id.edittext2); Listitem.add (map); } Simpleadapter simpleadapter=new simpleadapter (mainactivity.this, ListItem, R.layout.item, New string[]{"Detailname", "Detailmobile"}, New Int[]{r.id.edittext1,r.id.edittext2}); Lv.setadapter (Simpleadapter); Bind Adapter for ListView Lv.setonitemclicklistener (New Onitemclicklistener () { public void Onitemclick (adapterview<?> arg0, View arg1, int arg2,long arg3) { Intent intent=new Intent (); Intent.setclass (Mainactivity.this, Detailactivity.class); StartActivity (Intent); Finish (); } }); } @Override public boolean Oncreateoptionsmenu (Menu menu) { Getmenuinflater (). Inflate (R.menu.main, menu); return Super.oncreateoptionsmenu (menu); } public boolean onoptionsitemselected (MenuItem item) { Item.setchecked (TRUE); Switch (Item.getitemid ()) { Case R.ID.MENU1: Item.setchecked (TRUE); Intent intent=new Intent (); Intent.setclass (Mainactivity.this, Detailactivity.class); StartActivity (Intent); MainActivity.this.finish (); Break Case R.ID.MENU2: Item.setchecked (TRUE); Break Default Break } return super.onoptionsitemselected (item); } } detailactivity Code: public class Detailactivity extends activity{ private EditText name; Private EditText Mobil; Private EditText Email; Private EditText address; Private EditText Company; Private Myopenhelper Helper; Private Button savebtn; protected void OnCreate (Bundle savedinstancestate) { Super.oncreate (savedinstancestate); Setcontentview (R.layout.tvdetail); Name= (EditText) Findviewbyid (R.ID.EDITTEXT1); Mobil= (EditText) Findviewbyid (R.ID.EDITTEXT2); Email= (EditText) Findviewbyid (R.ID.EDITTEXT3); Address= (EditText) Findviewbyid (R.ID.EDITTEXT4); Company= (EditText) Findviewbyid (R.ID.EDITTEXT5); Savebtn= (Button) Findviewbyid (R.id.button1); Savebtn.setonclicklistener (New Onclicklistener () { public void OnClick (View arg0) { Helper=new Myopenhelper (detailactivity.this, "contacts.db", NULL, 1); Sqlitedatabase db = Helper.getreadabledatabase (); Get a readable database Contentvalues values = new Contentvalues (); String Name2=name.gettag (). toString (); String Mobile2=mobil.gettag (). toString (); String Email2=email.gettag (). toString (); String Addr2=address.gettag (). toString (); String Comp2=company.gettag (). toString (); if (Name2.equals ("") | | Mobile2.equals ("") | | Email2.equals ("") | | Addr2.equals ("")) { Toast.maketext (Detailactivity.this, "Please enter contact information", Toast.length_long). Show (); else { Cursor cursor=db.rawquery ("select * from Contacts where name=?", New String[]{name2}); if (Cursor.getcount () <=0) { Values.put ("Name", Name.gettext (). toString ()); Values.put ("mobile", Mobil.gettext (). toString ()); Values.put ("Email", Email.gettext (). toString ()); Values.put ("Address", Address.gettext (). toString ()); Db.insert ("Contacts", null, values); Values.clear (); Toast.maketext (Detailactivity.this, "saved successfully", Toast.length_long). Show (); }else { Toast.maketext (Detailactivity.this, "the contact already exists," Toast.length_long). Show (); } } }); } protected void OnDestroy () { Super.ondestroy ();} } Myopenhelper Code: public class Myopenhelper extends sqliteopenhelper{ public static final String create__contacts= "CREATE table CONTACTS (ID integer primary key autoincrement,name,mobile, email,address) "; Private Context Mycontext; Public Myopenhelper (Context context, String name, Cursorfactory factory, int version) { Super (context, name, Factory, version); Mycontext=context; } @Override public void OnCreate (Sqlitedatabase db) { Db.execsql (create__contacts); Toast.maketext (Mycontext, "Success", Toast.length_long). Show (); } public void Onupgrade (sqlitedatabase db, int arg1, int arg2) { TODO auto-generated Method Stub} } Run Result: () |