Android--CursorAdapter

Source: Internet
Author: User

CursorAdapter

CursorAdapter inherits from Baseadapter, which is a virtual class that provides a connecting bridge for the cursor and the ListView.

 Public Abstract   extends baseadapter

Note that the cursor must have a column named "_id". Like contacts._id for "_id."

The following functions must be implemented:

Abstract View Newview (context  context, cursor  cursor, viewgroup  parent)    abstract  void  BindView (View  View, context  context, cursor  cursor)

Newview This function is called after the first call, if the data is incremented, it is called again, but the redraw is not called .
Once the data is increased, call the function back to generate the view that corresponds to the new data.
After the BindView function is called the first time, the data update is called again , but the redraw is called again .

In general, you should call BindView if you find that the view is empty call Newview to generate the view.

Code
 Public classMysqliteopenhelperextendsSqliteopenhelper { PublicMysqliteopenhelper (Context context,intversion) {         Super(Context, "dianhuaben.db",NULL, version); } @Override Public voidonCreate (Sqlitedatabase db) {//Note: When using CursorAdapter, the CREATE table must have columns with column names of _idString sql = "CREATE TABLE dhb (_id INTEGER PRIMARY KEY autoincrement,name varchar (), phone VARCHAR (20))";     Db.execsql (SQL); } @Override Public voidOnupgrade (Sqlitedatabase db,intOldversion,intnewversion) {     } }
 Public voidcreatecursoradapter (cursor cursor) {//Leng Adapter, construction method, incoming cursorMadapter =NewCursorAdapter ( This, cursor) {//overriding two methods@Override PublicView Newview (context context, cursor cursor, viewgroup parent) {//find layouts and controlsViewholder holder =NewViewholder (); Layoutinflater Inflater=Getlayoutinflater (); View Inflate= Inflater.inflate (R.layout.listview_item,NULL); Holder.item_tv_name=(TextView) Inflate.findviewbyid (r.id.item_tv_name); Holder.item_tv_phone=(TextView) Inflate.findviewbyid (R.id.item_tv_phone);                 Inflate.settag (holder); returnInflate;//The returned view is passed to BindView. } @Override Public voidBindView (view view, context context, cursor cursor) {//set the data to the interfaceViewholder holder =(Viewholder) View.gettag (); String name= Cursor.getstring (Cursor.getcolumnindex ("name")); String Phone= Cursor.getstring (Cursor.getcolumnindex ("Phone"));                 Holder.item_tv_name.setText (name);             Holder.item_tv_phone.setText (phone);                                                         }                                                                   }; };
Simplecursoradapter

Simply mention

 Public void createsimplecursoradapter (cursor cursor)     {//        string[] from = {Table_name_name,table_name_phone};  int [] to = {R.id.item_tv_name,r.id.item_tv_phone};                   //         new simplecursoradapter (mainactivity.  this, R.layout.listview_item, cursor, from, to);     }

When using Simplecursoradapter, the CREATE table must have columns with column names in _id

I'm the dividing line of the king of the land Tiger.

Android--CursorAdapter

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.