"Android 9" simplecursoradapter, displaying data in a list

Source: Internet
Author: User

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

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.