My Custom CursorAdapter doesn't update my ListView -


i'm having troubles custom cursoradapter , listview, fact is, can save data in sqlite database in custom contentprovider listview not populated.

i know db operations heavy long operations, therefore in thread , furthermore cursorloader subclass of asynctaskloader, should prepared that.

with simplecursoradapter works fine custom cursoradapter not.

can tell me wrong , how solve it?

thanks in advance.

my code following

public class textnoteadapter extends cursoradapter {      /*********** declare used variables *********/      private cursor                 mcursor;     private context                mcontext;              private static  layoutinflater minflater=null;      /*************  textnoteadapter constructor *****************/     public textnoteadapter (context context, cursor cursor, int flags) {         super(context, cursor,flags);          minflater =(layoutinflater)context.getsystemservice(context.layout_inflater_service);         mcontext  = context;         mcursor   = cursor;                                }       @override     public view newview(context context, cursor cursor, viewgroup parent) {          //layoutinflater inflater = layoutinflater.from(parent.getcontext());         view view = minflater.inflate(r.layout.textnote_info, parent, false);         viewholder holder = new viewholder();          holder.note_name               = (textview)view.findviewbyid(r.id.note_name);         holder.creation_date           = (textview)view.findviewbyid(r.id.creation_date);         holder.modification_date       = (textview)view.findviewbyid(r.id.modification_date);         holder.label_creation_date     = (textview)view.findviewbyid(r.id.label_creation_date);         holder.label_modification_date = (textview)view.findviewbyid(r.id.label_modification_date);           view.settag(holder);          return view;     }           @override      public void bindview(view view, context context, cursor cursor) {          // here setting our data means, take data cursor , put in views          view vi = view;          viewholder holder = (viewholder)view.gettag();           if(view==null){                                /****** inflate textnote_info.xml file each row ( defined below ) *******/              vi = minflater.inflate(r.layout.textnote_info, null);              /************  set holder layoutinflater ************/              vi.settag( holder );           } else               holder=(viewholder)vi.gettag();                                      /************  set model values in holder elements ***********/          holder.note_name.settext(cursor.getstring(cursor.getcolumnindex(textnotesdb.key_note_name)));          holder.creation_date.settext(cursor.getstring(cursor.getcolumnindex(textnotesdb.key_creation_date)));          holder.modification_date.settext(cursor.getstring(cursor.getcolumnindex(textnotesdb.key_modification_date)));          holder.label_creation_date.settext(constants.label_creation_date);          holder.label_modification_date.settext(constants.label_modification_date);       }                 @override      protected void oncontentchanged() {           // todo auto-generated method stub           super.oncontentchanged();           notifydatasetchanged();      }                 /********* create holder class contain inflated xml file elements *********/      public static class viewholder{           public textview note_name;          public textview creation_date;          public textview modification_date;          public textview label_creation_date;          public textview label_modification_date;       }     } 

and here mainactivity

import android.app.activity; import android.app.loadermanager; import android.content.cursorloader; import android.content.intent; import android.content.loader; import android.database.cursor; import android.os.bundle; import android.os.handler; import android.util.log; import android.view.menu; import android.view.view; import android.widget.adapterview; import android.widget.adapterview.onitemclicklistener; import android.widget.button; import android.widget.cursoradapter; import android.widget.listview; import android.widget.toast;  public class mainactivity extends activity implements loadermanager.loadercallbacks<cursor>{       private cursor              cursor;     private button              addbutton;     private listview            listview;     private textnoteadapter     dataadapter;       @override     protected void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         setcontentview(r.layout.activity_main);               displaylistview();           addbutton = (button)findviewbyid(r.id.add_textnote);          addbutton.setonclicklistener(new view.onclicklistener() {              @override             public void onclick(view v) {                 // starts new intent add note                 intent intent = new intent(getbasecontext(), textnoteedit.class);                 bundle bundle = new bundle();                 bundle.putstring("mode", "add");                 intent.putextras(bundle);                 startactivity(intent);             }         });      }      @override     protected void onresume() {         super.onresume();         log.i("tag", "mainactivity:: onresume");         /** starts new or restarts existing loader in manager **/         getloadermanager().restartloader(0, null, this);     }       private void displaylistview() {      // ensures loader initialized , active.     // if loader specified id exists, last created loader reused.     // else  initloader() triggers loadermanager.loadercallbacks method oncreateloader().     // implement code instantiate , return new loader      getloadermanager().initloader(0, null, this);               // listview layout , initialize     listview = (listview) findviewbyid(r.id.textnote_list);      // db takes long, therefore operation should take place in new thread!                          new handler().post(new runnable() {                 @override                 public void run() {                     dataadapter = new textnoteadapter(mainactivity.this, null, cursoradapter.flag_register_content_observer);                     listview.setadapter(dataadapter);                     log.i("tag", "mainactivity:: handler... run()");                                        }             });                     listview.setonitemclicklistener(new onitemclicklistener() {             @override             public void onitemclick(adapterview<?> listview, view view, int position, long id) {                  /** cursor, positioned corresponding row in result set **/                 cursor cursor = (cursor) listview.getitematposition(position);                                 // display selected note                 string notename = cursor.getstring(cursor.getcolumnindexorthrow(textnotesdb.key_note_name));                  toast.maketext(getapplicationcontext(), notename, toast.length_short).show();                  string rowid = cursor.getstring(cursor.getcolumnindexorthrow(textnotesdb.key_rowid));                   // starts new intent update/delete textnote                 // pass in row id create content uri single row                 intent intent = new intent(getbasecontext(), textnoteedit.class);                 bundle bundle = new bundle();                 bundle.putstring("mode", "update");                 bundle.putstring("rowid", rowid);                 intent.putextras(bundle);                 startactivityforresult(intent, 1);              }         });      }          /** called when new loader needs created.**/     @override     public loader<cursor> oncreateloader(int id, bundle args) {         log.i("tag", "mainactivity:: oncreateloader");         string[] projection = {                              textnotesdb.key_rowid,                             textnotesdb.key_guid,                             textnotesdb.key_note_name,                             textnotesdb.key_note,                             textnotesdb.key_creation_date,                             textnotesdb.key_modification_date                            };      cursorloader cursorloader = new cursorloader(this, mycontentprovider.content_uri, projection, null, null, null);          return cursorloader;     }      @override     public void onloadfinished(loader<cursor> loader, cursor data) {         // swap new cursor in.  (the framework take care of closing         // old cursor once return.)                dataadapter.swapcursor(data);     }      @override     public void onloaderreset(loader<cursor> loader) {         // called when last cursor provided onloadfinished()         // above closed.  need make sure no         // longer using it.         dataadapter.swapcursor(null);     }      @override     public boolean oncreateoptionsmenu(menu menu) {         getmenuinflater().inflate(r.menu.activity_main, menu);         return true;     }   } 

as in comment below question, solve adding 2 lines. should this

@override public void onloadfinished(loader<cursor> loader, cursor data) {     // swap new cursor in.  (the framework take care of closing     // old cursor once return.)            dataadapter.notifydatasetchanged(); // <-     listview.setadapter(dataadapter);   // <-     dataadapter.swapcursor(data); } 

Comments

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

Python ctypes access violation with const pointer arguments -