Experiment 8 SQLite Database operations
Purpose
Design a Personal Address book, master the development of the database under the Android platform, the personal Address Book mainly includes the contact list and contact details and other interfaces.
Requirements
The main interface of the program is the directory of contacts that displays the name of the contact on the phone. Long press the contact's name to display details about the contact. Add and Remove buttons to add contacts and delete contacts.
"Experimental Environment"
1) PC Machine
2) Operating system: Windows XP
3) Software: Eclipse, jdk1.6,android Sdk,adt
"Experimental content and procedures"
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
String.xml Code:
<?xml version="1.0"encoding="Utf-8"?><resources> <stringName="app_name">contact</string> <stringName="action_settings">settings</string> <stringName="Hello_world">hello world!</string> <stringName="Name"> Name: </string> <stringName="Phone"> Fixed Tel: </string> <stringName="Mobile"> Mobile: </string> <stringName="Email"> e-mail: </string> <stringName="Post"> Zip Code: </string> <stringName="Addr"> Correspondence Address: </string> <stringName="Comp"> Company Name: </string> <stringName="Add"> Add </string> <stringName="Modify"> Modify </string> <stringName="Delete"> Delete </string> <stringName="Save"> Save </string> <stringName="message"> Confirm Delete this contact? </string> <stringName="OK"> Determine </string> <stringName="Cancel"> Cancel </string> <stringName="title"> Contact List </string></resources>
Mainactivity.java part of the code:
Public classMainactivity extends Activity {PrivateDbopenhelper Dbopenhelper; PrivateContantsadapter Contantsadapter; PrivateListView Dblistview; Privatecursor cursor; FinalintMenu_add =Menu.first; FinalintMenu_change = menu.first+1; PrivateList<string>IDs; PrivateList<string>names; PrivateList<string>phones; Alertdialog Dialog; @Override Public voidonCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.main); Dbopenhelper=NewDbopenhelper ( This); Dblistview=(ListView) Findviewbyid (R.id.db_listview); Refreshdbopenhelper (); Dblistview.setonitemclicklistener (NewOnitemclicklistener () { Public voidOnitemclick (adapterview<?> arg0, View arg1,intposition,LongArg3) {Intent Intent=NewIntent (); Intent.putextra ("cmd",0); String contants_id= IDs.Get(position);//0 for query contact, 1 for Add contactIntent.putextra ("ID", contants_id); Intent.setclass (mainactivity. This, Detailcontantsactivity.class); StartActivity (Intent); } }); Dblistview.setonitemlongclicklistener (NewOnitemlongclicklistener () { PublicBoolean Onitemlongclick (adapterview<?>arg0, View arg1, FinalintPositionLongArg3) {Dialog=NewAlertdialog.builder (mainactivity. This). Settitle ("Tip !!"). Setmessage ("Are you sure you want to delete this record? "). Setpositivebutton ("Determine", NewDialoginterface.onclicklistener () { Public voidOnClick (Dialoginterface Dialog,intWhichbutton) {String contants_id= IDs.Get(position); Dbopenhelper.deletecontants (contants_id); Dbopenhelper.close (); Toast.maketext (mainactivity. This,"deleting database, please later ... ", Toast.length_long). Show (); Refreshdbopenhelper (); Contantsadapter.notifydatasetchanged (); }}). Setnegativebutton ("Cancel",NewDialoginterface.onclicklistener () { Public voidOnClick (Dialoginterface Dialog,intWhichbutton) {Dialog.dismiss (); }}). Show (); return false; } }); } @Overrideprotected voidOnresume () {refreshdbopenhelper (); Contantsadapter.notifydatasetchanged (); Super.onresume (); } Public voidRefreshdbopenhelper () {cursor=dbopenhelper.selectcontants (); IDS=NewArraylist<string>(); Names=NewArraylist<string>(); Phones=NewArraylist<string>(); intCount =Cursor.getcount (); if(count>0){ for(intI=0; i<count;i++) {cursor.movetoposition (i); Ids.add (Cursor.getstring (0)); Names.add (Cursor.getstring (1)); Phones.add (Cursor.getstring (2)); } }Else{Toast.maketext ( ThisR.string. Not_dbcursor_values, Toast.length_short). Show (); } contantsadapter=NewContantsadapter ( This, Names,phones); Dblistview.setadapter (Contantsadapter); Cursor.close (); Dbopenhelper.close (); } @Override Publicboolean Oncreateoptionsmenu (Menu menu) {menu.add (0, Menu_add,0R.string. Menu_add). SetIcon (R.drawable.add); //Add-add_buttonMenu.add (0, Menu_change,0R.string. Menu_change). SetIcon (r.drawable.modify); //Add-add_change_password returnsuper.oncreateoptionsmenu (menu); } @Override Publicboolean onoptionsitemselected (MenuItem item) {Switch(Item.getitemid ()) {// CaseMenu_add://Press Change Add buttonIntent add_intent=NewIntent ( This, Detailcontantsactivity.class); Add_intent.putextra ("cmd",1); StartActivity (add_intent); Break; CaseMenu_change://Press Change Password buttonIntent change_password_intent =NewIntent (mainactivity. This, Passwordmanage.class); Startactivityforresult (Change_password_intent,0); Break; } returnsuper.onoptionsitemselected (item); }}
"Experimental Results"
"Summary of the experiment"
The experiment felt very difficult, because of missing a few classes, nothing to learn, but asked a few students, reluctantly completed a part.
Experiment 8 SQLite Database operations