java - Cursor Adapter selecting the wrong items to delete? -


so know question has been posted few times on site, case si little different.

suppose have these items in cursor (in order) :

alaska canada united states of america mongolia china india 

when click alaska, log shows mongolia clicked, , right after that, if select canada, shows mongolia clicked

and after click mongolia, united states of america clicked instead

it having weird behavior above. here code cursor adapter:

public class eventlistcursoradapter extends cursoradapter {     private layoutinflater cursorinflater;     calculations calculations = new calculations();     gson gson = new gson();     context appcontext;     databasehelper dbhelper;     context mcontext;     string[] whereclause;     intent mainactivityintent;      public eventlistcursoradapter(context context, cursor c, int flags) {         super(context, c, flags);         cursorinflater = (layoutinflater) context.getsystemservice(context.layout_inflater_service);         appcontext = context.getapplicationcontext();         mcontext = context;         notifydatasetchanged();     }     @override     public view newview(context context, cursor cursor, viewgroup parent) {         return cursorinflater.inflate(r.layout.card_view, parent, false);     }     @override     public void bindview(view view, context context, cursor cursor) {         textview timetext = (textview) view.findviewbyid(r.id.event_time);         textview nametext = (textview) view.findviewbyid(r.id.event_name);         textview datetext = (textview) view.findviewbyid(r.id.event_date);         textview summarytext = (textview) view.findviewbyid(r.id.event_summary);         textview locationtext = (textview) view.findviewbyid(r.id.event_location);         textview categorytext = (textview) view.findviewbyid(r.id.event_category);         cardview card = (cardview) view.findviewbyid(r.id.cardviewitem);          final cursor mcursor = cursor;          string date = calculations.unixtimeconverter(                 mcursor.getlong(mcursor.getcolumnindex(databasehelper.col_unixtime)         ))[0];         string time = calculations.unixtimeconverter(                 mcursor.getlong(mcursor.getcolumnindex(databasehelper.col_unixtime))         )[1];         final latlng location = gson.fromjson(mcursor.getstring(mcursor.getcolumnindex(databasehelper.col_location)),latlng.class);          nametext.settext(mcursor.getstring(mcursor.getcolumnindex(databasehelper.col_name)));          log.i("bindview cursor check", "name=" + mcursor.getstring((mcursor.getcolumnindex(databasehelper.col_name))));          datetext.settext(date);         timetext.settext(time);         summarytext.settext(mcursor.getstring(mcursor.getcolumnindex(databasehelper.col_summary)));         locationtext.settext(mcursor.getstring(mcursor.getcolumnindex(databasehelper.col_locationname)));         categorytext.settext(mcursor.getstring(mcursor.getcolumnindex(databasehelper.col_category)));          locationtext.setonclicklistener(new view.onclicklistener(){             @override             public void onclick(view v) {                 final cameraposition camlocation  = cameraposition.builder().target(location).zoom(18).build();                 mmap.animatecamera(cameraupdatefactory.newcameraposition(camlocation));             }         });          summarytext.setonclicklistener(new view.onclicklistener() {             @override             public void onclick(view view) {                 layoutinflater mlayoutinflator;                 mlayoutinflator = layoutinflater.from(appcontext);                 final alertdialog.builder mbuilder = new alertdialog.builder(mcontext);                 view mview = mlayoutinflator.inflate(r.layout.summarydialog,null);                  textview textview = mview.findviewbyid(r.id.maintext);                 textview.settext(                         mcursor.getstring(mcursor.getcolumnindex(databasehelper.col_summary))                 );                  textview.setmovementmethod(new scrollingmovementmethod());                  mbuilder.setview(mview);                 final alertdialog dialog = mbuilder.create();                 dialog.getwindow().setbackgrounddrawable(new colordrawable(color.transparent));                 dialog.show();             }         });          card.setonlongclicklistener(new view.onlongclicklistener() {             @override             public boolean onlongclick(view view) {                  whereclause = new string[] {string.valueof(mcursor.getlong(mcursor.getcolumnindex(databasehelper.col_localid)))};                 log.v("where clause:",whereclause[0]);                  log.i("event onlongclick", "name=" + mcursor.getstring(mcursor.getcolumnindex(databasehelper.col_name)));                  layoutinflater mlayoutinflator;                 mlayoutinflator = layoutinflater.from(appcontext);                 final alertdialog.builder mbuilder = new alertdialog.builder(mcontext);                 view mview = mlayoutinflator.inflate(r.layout.canceldelete_editor,null);                  final textview cancelbutton = (textview) mview.findviewbyid(r.id.cancelaction);                 final textview deletebutton = (textview) mview.findviewbyid(r.id.deleteentryaction);                  mbuilder.setview(mview);                 final alertdialog dialog = mbuilder.create();                  dialog.show();                 dbhelper = new databasehelper(mcontext);                 final sqlitedatabase db = dbhelper.getwritabledatabase();                  dialog.setondismisslistener(new dialoginterface.ondismisslistener() {                     @override                     public void ondismiss(dialoginterface dialoginterface) {                         log.v("where clause changed:",whereclause[0]);                     }                 });                 cancelbutton.setonclicklistener(new view.onclicklistener() {                     @override                     public void onclick(view view) {                         dialog.dismiss();                     }                 });                 log.v("item lond clicked",mcursor.getstring(mcursor.getcolumnindex(databasehelper.col_name)));                 deletebutton.setonclicklistener(new view.onclicklistener() {                     @override                     public void onclick(view view) {                          int tru = db.delete(databasehelper.table_name, databasehelper.col_localid + " = " + mcursor.getint(mcursor.getcolumnindex(databasehelper.col_localid)), null);                         log.v("deleting item:",mcursor.getstring(mcursor.getcolumnindex(databasehelper.col_name))+"");                          if(tru == 1){                             log.v("delete: ", "successfull!" + mcursor.getstring(mcursor.getcolumnindex(databasehelper.col_name)));                         }else{                             log.v("delete: ", "failed!" + mcursor.getstring(mcursor.getcolumnindex(databasehelper.col_name)));                         }                          datasendertoserver datasendertoserver = new datasendertoserver();                         datasendertoserver.eraseentry(mcursor.getstring(mcursor.getcolumnindex(databasehelper.col_globalid)));                         mainactivityintent = new intent(mcontext,mainactivity.class);                         mcontext.startactivity(mainactivityintent);                         dialog.dismiss();                         mcursor.requery();                     }                 });                 return true;             }         }); 

how choose right item delete cursor adapter? appreciated...

a cursor result set database position pointer current row. scroll list , new rows filled in based on cursor data, position pointer automatically moved under covers.so save cursor position in bindview method.

change onclicklistener in bindview blow

@override     public void bindview(view view, context context, cursor cursor) {      final int position = cursor.getposition();       deletebutton.setonclicklistener(new view.onclicklistener() {                         @override                         public void onclick(view view) {                           cursor.movetoposition(position);                             //now delete operation                          }                     });  } 

Comments

Popular posts from this blog

Sort a complex associative array in PHP -

vb.net - How to ignore if a cell is empty nothing -

recursion - Can every recursive algorithm be improved with dynamic programming? -