Small problems related to Simplecursoradapter

Source: Internet
Author: User

Today, when learning SQLite, the use of Simplecursoradapter.simplecursoradapter is specifically designed to connect to the database and view of the production. He is the bridge that displays the data from the database tables to the ListView. Following the online tutorial, I use the ListView to embody the usage of simplecursoradapter.

1ListView list=(ListView) Findviewbyid (R.id.listview);2Sqlitedatabase dbread=db.getreadabledatabase ();3Cursor cur=dbread.query ("User",NULL,NULL,NULL,NULL,NULL,NULL);4Adapter=NewSimplecursoradapter ( This, R.layout.layout,cur,Newstring[]{"name", "Sex"},New int[]{r.id.name,r.id.sex}];5Listview.setadapter (SCA);

However, when debugging the application error, column ' _id ' does not exist. Then only to see the tutorial write must request our database has a column, _id; Simplecursoradapter very proud, If you don't have this column, you won't do it .

There are two problems, a horizontal line appears in the middle of 1.SimpleCursorAdapter, which indicates that the function is obsolete and what is the replacement function. 2. When database data is updated, the list is not updated with adapter.notifydatasetchanged () , how do you keep the UI up to date?

The functions found in place of the Simplecursoradapter constructor are: Simplecursoradapter (context context, int layout, Cursor C, string[] from, int[) to , int flags) is just a few more flags than the previous one. The flags here are used to identify if the data changes when the call to Oncontentchanged () is notified of the ContentProvider data change. The correspondence has two constants: CursorAdapter. Flag_auto_requery and Cursoradapter.flag_register_content_observer. The former is not recommended for use after api11, it is not in the narrative. The latter function is to register a content monitor on the Cursor and invoke the Oncontentchanged () method when it notifies.

If you do not need to listen for contentprovider changes, or if you use Cursorloader in CursorAdapter (he will register a content monitor for you), you can pass 0.

In terms of UI updates, I found three methods:

Cursor.requery (); adapter.notifydatasetchanged ();

Adapter.swapcursor (newcursor); adapter.notifydatasetchanged ();

Adapter.changecursor (newcursor); adapter.notifydatasetchanged ();

In the first method, the Requery was drawn with a horizontal line, and the method failed to test.
The second and third methods are successful. For information, the differences between the two methods of Swapcursor and Changecursor are described below:

1  Publiccursor swapcursor (cursor newcursor) {2 if(Newcursor = =mcursor) {3 return NULL;4 }5Cursor Oldcursor =Mcursor;6 if(Oldcursor! =NULL) {7 if(Mchangeobserver! =NULL) Oldcursor.unregistercontentobserver (mchangeobserver);8 if(Mdatasetobserver! =NULL) Oldcursor.unregisterdatasetobserver (mdatasetobserver);9 }TenMcursor =Newcursor; One if(Newcursor! =NULL) { A if(Mchangeobserver! =NULL) Newcursor.registercontentobserver (mchangeobserver); - if(Mdatasetobserver! =NULL) Newcursor.registerdatasetobserver (mdatasetobserver); -Mrowidcolumn = Newcursor.getcolumnindexorthrow ("_id"); theMdatavalid =true; - //notify the observers about the new cursor - notifydatasetchanged (); -}Else { +Mrowidcolumn =-1; -Mdatavalid =false; + //notify the observers about the lack of a data set A notifydatasetinvalidated (); at } - returnOldcursor; - }

Swapcursor swapped a new cursor, returning the old cursor, which did not close the old cursor

1  Public void changecursor (cursor cursor) {2         Cursor old = swapcursor (cursor); 3         if NULL ) {4            old.close (); 5         }6     }


The changecursor replaces the original cursor and closes it.

If you use Cursorloader (which is really a good thing), it manages the cursor and does not need us to close the Cursor,loader to finish. We only need to implement the following three ways to

1 //called when a new Loader needs to be created2  PublicLoader<cursor> Oncreateloader (intID, Bundle args) {3 //Now Create and return a Cursorloader4 //creating a Cursor for the data being displayed.5 return NewCursorloader ( This, ContactsContract.Data.CONTENT_URI,6PROJECTION, SELECTION,NULL,NULL);7 }8 9 //called when a previously created loader have finished loadingTen  Public voidOnloadfinished (loader<cursor>loader, Cursor data) { One //Swap the new cursor in. (The framework would take care of closing the A //Old cursor once we return.) - madapter.swapcursor (data); - } the  - //called when a previously created loader is reset, making the data unavailable -  Public voidOnloaderreset (loader<cursor>loader) { - //This was called when the last Cursor provided to onloadfinished () + //Above is on to being closed. We need to make sure we is no - //longer using it. +Madapter.swapcursor (NULL);

Reference: http://stackoverflow.com/questions/11093380/ What-to-set-cursoradaptercontext-context-cursor-c-int-flags-to-in-order-to-m
http://www.blogc.at/2014/03/03/swapcursor-vs-changecursor-whats-the-difference/

Http://developer.android.com/reference/android/widget/CursorAdapter.html#CursorAdapter (Android.content.Context , android.database.Cursor, int)

Small problems related to Simplecursoradapter

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.