Use of Android CursorAdapter

Source: Internet
Author: User

The CursorAdapter inherits from the Baseadapter, providing a bridge for cursor and ListView connections.

First look at the cursoradapter part of the source code:

/**     * @seeandroid.widget.listadapter# getView (int, View, viewgroup)*/      PublicView GetView (intposition, View Convertview, ViewGroup parent) {         if(!mdatavalid) {             Throw NewIllegalStateException ("This should only being called when the cursor is valid"); }         if(!mcursor.movetoposition (position)) {             Throw NewIllegalStateException ("couldn ' t move cursor to position" +position);         } View v; if(Convertview = =NULL) {v=Newview (Mcontext, mcursor, parent); } Else{v=Convertview;         } bindview (V, Mcontext, mcursor); returnv; }  

It can be seen that CursorAdapter is the GetView method that inherits the Baseadapter and then calls the Newview and BindView methods in the GetView method, and we must implement its two methods when we write CursorAdapter.

 Public Abstract View Newview (context context, cursor cursor, viewgroup parent);      Public Abstract void BindView (view view, context context, cursor cursor);  
From the source can be seen:
    • Newview (): Not every time it is called, it is called only when instantiated, and when the data is incremented, it is not called when redrawing (such as modifying the contents of TextView in an entry).
    • 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.

Example part code that inherits CursorAdapter:

@Override PublicView Newview (context context, cursor cursor, viewgroup parent) {Viewholder Viewholder=NewViewholder (); 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); returnview; } @Override Public voidBindView (view view, context context, cursor cursor) {Log. I ("Cursor", "bindview=" +view); Viewholder Viewholder=(Viewholder) View.gettag (); //querying the Name field from the databaseString name=cursor.getstring (Cursor.getcolumnindex (personinfo.name)); //querying phone fields from the databaseString phonenumber=cursor.getstring (Cursor.getcolumnindex (Personinfo.phonenumber)); Viewholder.             Tv_name.settext (name); Viewholder.       Tv_phonenumber.settext (PhoneNumber); }  

Disadvantages:

    1. Exposing the cursor directly to the UI layer and writing a lot cursor.getString(cursor.getColumnIndex("scheme")) of similar code. Exposing the cursor directly to the UI layer is a bad demonstration, and the cursor is a very low-level data model that should not be exposed to the UI layer.
    2. SQLite loading more than 1M of data will become slow. Once the volume of data is large, the speed of the entire UI is reduced immediately.
    3. Recyclerview does not support CursorAdapter

In short, cursoradapter this way is not a good design model.

Use of Android CursorAdapter

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.