Experiment 8 SQLite Database operations

Source: Internet
Author: User
Tags gettext sqlite sqlite database stub

Lab Report

Course Name

Mobile Internet development based on Android platform

Date of experiment

2016/5/6

Name of the experimental project

SQLite database Operations

Location of the experiment

General Laboratory

Type of experiment

-Verification Type √ design type-comprehensive type

Hours

2

The purpose and requirements of the experiment (the knowledge points which are involved in this experiment and are required to be mastered)

1. Design a Personal Address book, mastering the development of the database under the Android platform, the personal Address Book mainly includes the contact list and contact details and other interfaces.

2. The main interface of the program is the directory of the contacts to display the name of the contact on the phone. Click the contact's name to display the contact's details. The menu bar pops up after you press the MEMU key. Click the buttons on the menu bar to add contacts and delete contacts.

second, the experimental environment (the hardware equipment and related software used in this experiment)

(1) PC Machine

(2) Operating system: Windows XP

(3) Software: Eclipse, jdk1.6,android Sdk,adt

Third, the contents and steps of the experiment

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

Four, the experimental results (the experimental source program list and operating results or experimental conclusions, experimental design drawings)

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: ()

Five, Experimental summary (analysis of the results of the experiment, experience and improvement of experimental ideas)

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.

Experimental reviews

Experimental results

Guide Teacher Signature: Date

Experiment 8 SQLite Database operations

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.