1) Contact. java
Package com. example. l0831_sqlite_all;/*** Contact class, encapsulate data * @ author asus */public class Contact {private Integer _ id; private String name; private String phone; public Contact () {super ();} public Contact (String name, String phone) {this. name = name; this. phone = phone;} public Contact (int id, String name, String phone) {this. _ id = id; this. name = name; this. phone = phone;} public Integer get_id () {return _ id;} public void set_id (Integer id) {_ id = id;} public String getName () {return name ;} public void setName (String name) {this. name = name;} public String getPhone () {return phone;} public void setPhone (String phone) {this. phone = phone ;}@ Override public String toString () {return "Contants [id =" + _ id + ", name =" + name + ", phone = "+ phone +"] ";}}
2) MyOpenHelper. java
Package com. example. l0831_sqlite_all; import android. content. context; import android. database. sqlite. SQLiteDatabase; import android. database. sqlite. SQLiteOpenHelper; public class MyOpenHelper extends SQLiteOpenHelper {private static final String name = "contants"; // database name private static final int version = 1; // database version public MyOpenHelper (Context context) {/*** third parameter: * CursorFactory specifies Factory type. * If it is set to null, the default factory class is used. */Super (context, name, null, version) ;}@ Override public void onCreate (SQLiteDatabase db) {// create a contacts table, the field type and length provided in SQL expressions only improve code readability. Db.exe cSQL ("create table if not exists contacts (" + "_ id integer primary key autoincrement," + "name varchar (20)," + "phone varchar (50 )) ") ;}@ Override public void onUpgrade (SQLiteDatabase db, int oldVersion, int newVersion) {// only used for demonstration, So delete the table before creating it. Db.exe cSQL ("drop table if exists contacts"); this. onCreate (db );}}
3) ContactService. java
Package com. example. l0831_sqlite_all; import java. util. arrayList; import java. util. list; import android. content. context; import android. database. cursor;/*** the function of this class is to encapsulate the methods used to operate the database * @ author asus */public class ContactsService {private MyOpenHelper openHelper; // constructor, public ContactsService (Context context) {this. openHelper = new MyOpenHelper (context);}/*** save ** @ param c Ontact */public void save (Contact contact ){//? String SQL = "INSERT INTO contacts (name, phone) VALUES (?, ?) "; Object [] bindArgs = {contact. getName (), contact. getPhone ()}; // obtain the database write permission and execute the inserted SQL statement this.openhelper.getwritabledatabase(cmd.exe cSQL (SQL, bindArgs );} /*** find * @ param id * @ return */public Contact find (Integer id) {String SQL = "SELECT _ id, name, phone FROM contacts WHERE _ id =? "; String [] selectionArgs = {id +" "}; // query the data and put it in the Cursor object. // obtain the database write permission and execute the query statement rawQuery) cursor cursor = this. openHelper. getReadableDatabase (). rawQuery (SQL, selectionArgs); if (cursor. moveToFirst () // If the cursor is not empty, return new Contact (cursor. getInt (0), cursor. getString (1), cursor. getString (2); return null;}/*** update * @ param contact */public void UPDATE (Contact contact) {String SQL = "update contact S SET name = ?, Phone =? WHERE _ id =? "; Object [] bindArgs = {contact. getName (), contact. getPhone (), contact. get_id ()}; this.openhelper.getwritabledatabase(cmd.exe cSQL (SQL, bindArgs);}/*** delete * @ param id */public void delete (Integer id) {String SQL = "DELETE FROM contacts WHERE _ id =? "; Object [] bindArgs = {id}; this.openhelper.getreadabledatabase(.exe cSQL (SQL, bindArgs);}/*** get record count ** @ return */public long getCount () {String SQL = "SELECT count (*) FROM contacts"; Cursor cursor = this. openHelper. getReadableDatabase (). rawQuery (SQL, null); cursor. moveToFirst (); return cursor. getInt (0);}/*** retrieve paging data * @ param startIndex * @ param maxCount * @ return */public List <Contact> g EtScrollData (long startIndex, long maxCount) {String SQL = "SELECT _ id, name, phone FROM contacts LIMIT ?,? "; String [] selectionArgs = {String. valueOf (startIndex), String. valueOf (maxCount)}; Cursor cursor = this. openHelper. getReadableDatabase (). rawQuery (SQL, selectionArgs); List <Contact> contacts = new ArrayList <Contact> (); while (cursor. moveToNext () {Contact contact = new Contact (cursor. getInt (0), cursor. getString (1), cursor. getString (2); contacts. add (contact) ;}return contacts;}/*** obtain the paging data and provide it to Si Use mpleCursorAdapter. * @ Param startIndex * @ param maxCount * @ return */public Cursor getScrollDataCursor (long startIndex, long maxCount) {String SQL = "SELECT _ id, name, phone FROM contacts LIMIT ?,? "; String [] selectionArgs = {String. valueOf (startIndex), String. valueOf (maxCount)}; Cursor cursor = this. openHelper. getReadableDatabase (). rawQuery (SQL, selectionArgs); return cursor ;}}
4) MainActivity. java
Package com. example. l0831_sqlite_all; import android. app. activity; import android. database. cursor; import android. OS. bundle; import android. view. view; import android. widget. adapterView; import android. widget. adapterView. onItemClickListener; import android. widget. listView; import android. widget. simpleCursorAdapter; import android. widget. toast; public class MainActivity extends Activity {@ Override protected v Oid onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); // create the object operation database of ContactsService) ContactsService contactsService = new ContactsService (this); Contact contact = new Contact (1001, "Tana", "18867899086"); contactsService. save (contact); contactsService. find (1001); // obtain the paging data and provide it to SimpleCursorAdapter Using Cursor cursor = contactsService. getScrollDa TaCursor (0, 3); // obtain ListView lv = (ListView) this. findViewById (R. id. listView); // create SimpleCursorAdapter adapter = new SimpleCursorAdapter (this, R. layout. contact_cell, // resource file cursor referenced by ListView, // cursor object for Retrieving Database Data new String [] {"_ id", "name", "phone "}, // encapsulate the new int [] {R. id. TV _id, R. id. TV _name, R. id. TV _phone}); // encapsulate the Control id in the ListView resource file // set the ListView adapter lv. setAdapter (Dapter); // Add event lv. setOnItemClickListener (new OnItemClickListener () {@ Override public void onItemClick (AdapterView <?> Parent, View view, int position, long id) {Cursor cursor = (Cursor) parent. getItemAtPosition (position); Toast. makeText (MainActivity. this, cursor. getString (1), Toast. LENGTH_SHORT ). show ();}});}}
5) running result:
650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/103HR3Q-0.jpg "title =" Capture. JPG "/>
This article is from the MySpace blog, please be sure to keep this source http://wangzhaoli.blog.51cto.com/7607113/1286440