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