Simplecursoradapter
Simplecursoradaper is similar to Simpleadapter, but the data source is the cursor.
Operation Steps:
Show data in a list
1 PublicMysqliteopenhelper (Context context) {2 Super(Context, "person.db",NULL, 1);3 }4 Public voidonCreate (Sqlitedatabase db) {5Db.execsql ("CREATE table if not exists person ("6+ "_id Integer primary key AutoIncrement,"7+ "name varchar (20)");8 }9 /**Query MethodTen * @returnCursor Object One */ A PublicCursor Query () { -Sqlitedatabase db=getwritabledatabase (); -Cursor cursor=db.query ("person",Newstring[]{"id _id", "Name"},NULL,NULL,NULL,NULL,NULL); the returncursor; - } - /**inserting data into the database*/ - Public voidinsert (String [] args) { +Sqlitedatabase db= This. Getwritabledatabase (); - //the object can manipulate key-value pairs of data +Contentvalues values=Newcontentvalues (); AValues.put ("name", Args[0]);//Storing Data at //table name, forcing NULL to insert a column name that is worth the data column, record the data -Db.insert ("Person",NULL, values); -}Mysqliteopenhelper Code
1 Activity Code2 Public classMainextendsActivityImplementsonclicklistener{3 PrivateButton addbtn;4 PrivateButton findbtn;5 PrivateEditText Nameet;6 PrivateEditText Phoneet;7 PrivateListView ListView;8 PrivateSimplecursoradapter Adapter;9 PrivateMysqliteopenhelper Helper;Ten Public voidonCreate (Bundle savedinstancestate) { One Super. OnCreate (savedinstancestate); A Setcontentview (r.layout.main); -addbtn=(Button) Findviewbyid (R.ID.ADDBTN); -findbtn=(Button) Findviewbyid (R.ID.FINDBTN); thenameet=(EditText) Findviewbyid (r.id.nameet); -phoneet=(EditText) Findviewbyid (r.id.phoneet); -listview=(ListView) Findviewbyid (r.id.lv); -Addbtn.setonclicklistener ( This); +Findbtn.setonclicklistener ( This); - +Helper=NewMysqliteopenhelper ( This); ACursor cursor=helper.query (); atAdapter=NewSimplecursoradapter ( This, R.layout.list_item, -CursorNewstring[]{"_id", "name", "Phone"}, - New int[]{r.id.idtv,r.id.nametv,r.id.phonetv}]; - Listview.setadapter (adapter); - } - Public voidOnClick (View v) { inMysqliteopenhelper db=NewMysqliteopenhelper (Main. This); - Switch(V.getid ()) { to Caser.id.addbtn: +String name=Nameet.gettext (). toString (); -String phone=Phoneet.gettext (). toString (); theDb.insert (Newstring[]{name,phone}); *Toast.maketext ( This, "Insert succeeded", 3000). Show (); $ Break;Panax Notoginseng Caser.id.findbtn: -Cursor cur=helper.query (); theAdapter.changecursor (cur);//Update Leng Adapter Object +Adapter.notifydatasetchanged ();//Notification list control data changes A Break; the}Activity Code
"Android 9" simplecursoradapter, displaying data in a list