Experiment 8 SQLite Database operations

Source: Internet
Author: User
Tags gettext sqlite sqlite database

Lab Report

Course Name

Experiment 8 SQLite Database operations

Date of experiment

2016.5.6

Name of the experimental project

Multimedia application Development

Location of the experiment

S30010

Type of experiment

-Verification Type √ design type-comprehensive type

Hours

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

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. The main interface of the program is the directory of contacts that displays 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 button 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, experiment contents and steps

(1) Determine the data structure of the database.

(2) Define string resource String.xml

(3) Development layout file Activity_main.xml is used to display the contact list.

(4) Create a new tvdetail.xml in the layout directory to display contact details.

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

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

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

This test is not complete, layout can be achieved, in the function of adding contacts, methods and code are basically understood and written, but in the run time crashes, still in the further debugging.

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.