I. Introduction of CursorAdapter
CursorAdapter This class is inherited from Baseadapter and it is a virtual class it provides a bridge for cursor and ListView connections
Second, CursorAdapter detailed
inheritance diagram for 1.CursorAdapter
It can be seen that CursorAdapter is inherited from Baseadapter, and it has a direct subclass Simplecursoradapter
usage of 2.CursorAdapter
Let's first look at some of the source code for CursorAdapter:
/** * @see android.widget.listadapter#getcount () */public int GetCount () {if (Mdatavalid && m Cursor! = null) {return mcursor.getcount (); } else {return 0; }}/** * @see android.widget.listadapter#getitem (int) */public Object getItem (int position) { if (mdatavalid && mcursor! = null) {mcursor.movetoposition (position); return mcursor; } else {return null; }}/** * @see android.widget.listadapter#getitemid (int) */public long getitemid (int position) { if (mdatavalid && mcursor! = null) {if (mcursor.movetoposition (position)) {return MC Ursor.getlong (Mrowidcolumn); } else {return 0; }} else {return 0; }}/** * @see android.widget.listadapter# getView (int, View, viewgroup) */public View GetView (int position, View Convertview, ViewGroup parent) {if (!mdatavalid) {throw new IllegalStateException ( "This should was called when the cursor was valid"); } if (!mcursor.movetoposition (position)) {throw new IllegalStateException ("couldn ' t move cursor to POS Ition "+ position); } View v; if (Convertview = = null) {v = Newview (Mcontext, mcursor, parent); } else {v = convertview; } bindview (V, Mcontext, mcursor); return v; }
from the source code can be seen CursorAdapter is inherited baseadapter after the GetView method to overwrite it in the GetView method called Newview and BindView method, we write CursorAdapter There are two ways to implement it
/** * makes a new view to the data pointed through cursor. * @param context Interface to application's global information * @param cursor the cursor from which to get the data. The cursor is already * moved to the correct position. * @param parent the parent to which the new view was attached to * @return the newly created view. * /public abstract View Newview (context context, cursor cursor, viewgroup parent); /** * Bind an existing view to the data pointed through cursor * @param view Existing view, returned earlier by new View * @param context Interface to application ' s global information * @param cursor, the cursor from which to get The data. The cursor is already * moved to the correct position. * /public abstract void BindView (view view, context context, cursor cursor);
from the source GetView ( int position, View Convertview, ViewGroup Parent) method we can see: (1)Newview: Not every time is called, it is only called when instantiated, the data will be added when the time is also called, but in redrawing (such as modifying the contents of the TextView) Will not be called when the (2) BindView: It can be seen from the code that the BindView method must be called before the item is drawn, which is also called when it is redrawn.
3.CursorAdapter There's another important way. Public voidchangecursor ( cursor cursor) The source code is as follows:
/** * Change the underlying cursor to a new cursor. If There is an existing cursor it'll be * closed. * * @param cursor The new cursor to be used */public void changecursor (cursor cursor) {cursor old = s Wapcursor (cursor); if (old! = null) {old.close (); }} swapcursor (Cusor) has the following source code:/** * Swap in a new cursor, returning the old cursor. Unlike * {@link #changeCursor (cursor)}, the returned old cursor is <em>not</em> * closed. * * @param newcursor The new cursor to be used. * @return Returns the previously set Cursor, or null if there wasa not one. * If The given new Cursor is the same instance is the previously set * Cursor, and NULL is also returned. */Public Cursor swapcursor (cursor newcursor) {if (newcursor = = mcursor) {return null; } Cursor oldcursor = Mcursor; if (oldcursor! = null) {if (mchangeobserver! = null) oldcursor. Unregistercontentobserver (Mchangeobserver); if (mdatasetobserver! = null) oldcursor.unregisterdatasetobserver (mdatasetobserver); } mcursor = Newcursor; if (newcursor! = null) {if (mchangeobserver! = null) newcursor.registercontentobserver (mchangeobserver); if (mdatasetobserver! = null) newcursor.registerdatasetobserver (mdatasetobserver); Mrowidcolumn = Newcursor.getcolumnindexorthrow ("_id"); Mdatavalid = true; Notify the observers about the new cursor notifydatasetchanged (); } else {mrowidcolumn =-1; Mdatavalid = false; Notify the observers about the lack of a data set notifydatasetinvalidated (); } return oldcursor; }
it can be seen from the source code that this method is called and the current mcursor is placed as a new cursor. Return the original cursor and turn it off
function: This method is called when our cursor changes adapter.changecursor (cursor), which functions likeadapter.notifydatasetchanged () method4.Before the doubts
I've been wondering how the cursor is moving, for example, there are 40 of data in the cursor, then it is how to move the cursor in one row to show the 40 data, after reading the source, it is very simple,it inin the GetCount () methodreturn Mcursor. GetCount (); and then when the GetView method is calledMcursor. Movetoposition (position) is actually the same as the baseadapter principle, so that you can draw a piece of the article.
third, the source of small cases:1. Case function
Enter your name and phone number in EditText, and click Save to display it in the listview below.
2. Code Snippets(1) The main code of Mycursoradapter:
@Override public view Newview (context context, cursor cursor, viewgroup parent) {Viewholder view Holder= new Viewholder (); Layoutinflater inflater= (Layoutinflater) Context.getsystemservice (Context.layout_inflater_service); View view=inflater.inflate (r.layout.item_contacts, Parent,false); Viewholder. Tv_name= (TextView) View.findviewbyid (r.id.tv_showusername); Viewholder. Tv_phonenumber= (TextView) View.findviewbyid (R.id.tv_showusernumber); View.settag (Viewholder); Log. I ("cursor", "newview=" +view); return view; } @Override public void BindView (view view, context context, cursor cursor) {Log. I ("cursor", "Bin Dview= "+view); Viewholder viewholder= (Viewholder) View.gettag (); Query the last Name field from the database String name=cursor.getstring (Cursor.getcolumnindex (personinfo.name)); Query the phone field from the database String phonenumber=cursor.getstring (cursor.getcolumNIndex (Personinfo.phonenumber)); Viewholder. Tv_name.settext (name); Viewholder. Tv_phonenumber.settext (PhoneNumber); }call the Newview method to instantiate the entry, and then call BindView to draw the entry, and the Newview method is not called when drawing only.
(2) Click the Save button to execute the method
private void Setclicklistener () {Btn_save.setonclicklistener (new Onclicklistener () { public void OnClick (View v) {username=et_name.gettext (). toSt Ring (); Userphonenumber=et_phonenumber. GetText (). toString (); if (Username.equals (")") {Toast. Maketext (mainactivity.this, "User name cannot be empty!", 0). Show (); Return } if (Userphonenumber.equals (")") {Toast. Maketext (Mainactivity.this, "Phone cannot be empty", 0). Show (); Return } contentvalues contentvalues= new Contentvalues (); Contentvalues.put (PersonInfo. NAME, UserName); Contentvalues.put (Personinfo.phonenumber, Userphonenumber); Insert text from EditText into the database database.insERT (PersonInfo. Person_info_table, null,contentvalues); Based on the _id descending socialize database, the last inserted Cursor mycursor = Database.query (PersonInfo) is guaranteed. Person_info_table, NULL, NULL, NULL, NULL, NULL, by-and-by); Cursor Change call Chanagecursor () method Mycursoradapter.changecursor (MyCursor); } }); }
If the database operation is not familiar can see this blog
Source Stamp here
Use of CursorAdapter in Android