android - Refresh list view when button delete is clicked inside custom adapter -


i have searched issue in web, nothing has helped me yet. many people ask same thing none answers problem. have listview button delete each row of list. have every thing done, except list view updated. list view populated information stored in database. each row, have custom listadapter and, handle onclick event of delete button inside class.

so main activity set custom adapter:

public class clientsactivity extends listfragment {  private cursor mcursor;      @override public view oncreateview(layoutinflater inflater, viewgroup container,         bundle savedinstancestate) {      view view = inflater.inflate(r.layout.activity_clients, container, false);      listemployees(view);              ...     }      private void listemployees(view view){     //get list db , set other variables..     ...              listview lvemployees = (listview) view.findviewbyid (android.r.id.list);     listclientcursoradapter notes = new listclientcursoradapter(context, r.layout.activity_row_client, mcursor, from, to, 0);      lvemployees.setadapter(notes);      } } 

and custom adapter class:

public class listclientscursoradapter extends simplecursoradapter{  //set constructor , stuff ... @override public view newview(context context, cursor cursor, viewgroup parent) {      final layoutinflater inflater = layoutinflater.from(context);     view v = inflater.inflate(layout, parent, false);      //things populate rows             ...      imagebutton buttondelete = (imagebutton)  v.findviewbyid(r.id.imagebuttondelete);      //i set in bundle, id of employee want delete.     final bundle marguments = new bundle();     marguments.putstring("id", id);      buttondelete.setonclicklistener(new view.onclicklistener() {          @override         public void onclick(view v){              alertdialog.builder alert = new alertdialog.builder(v.getcontext());              alert.setmessage("are sure?");              alert.settitle("deleting..");               alert.setnegativebutton("cancel", new dialoginterface.onclicklistener() {                 public void onclick(dialoginterface dialog, int whichbutton) {                     dialog.cancel();                 }});              alert.setpositivebutton("ok", new dialoginterface.onclicklistener() {                 public void onclick(dialoginterface dialog, int whichbutton) {                                             //i bundle id of employee                     string id = marguments.getstring("id");                      deleteemployee(id);                 }             });              alertdialog alertdialog = alert.create();             alertdialog.show();          }});     return v; }  private void deleteemployee(string id) {     //here delete item in db             //and works fine ..             ... //solution:             //here have refresh list. how             this.changecursor(db.listcompanies(context));             this.notifydatasetchanged(); } } 

as know notifydatasetchanged() works if data list array. not case.

any apreciated!!.

solution: inside method deleteemployee put following code, (as can see in code above):

this.changecursor(db.listemployees(context)); this.notifydatasetchanged(); 

where

db.listemployees(context)  

is method use retrieve values db populate listview.

so, can see, needed change old cursor new 1 data updated.

you requery mcursor , call notifydatasetchanged()


Comments

Popular posts from this blog

c# - DetailsView in ASP.Net - How to add another column on the side/add a control in each row? -

javascript - firefox memory leak -

Trying to import CSV file to a SQL Server database using asp.net and c# - can't find what I'm missing -