android - listview not refresh properly after call notifyDataSetChanged() -


i've facing problem refresh listview after delete item database.

problem when i'm call notifydatasetchanged() return listview removing last index element list instead of specific index every time (eg want delete 2nd index item after deleting item listview show removing last item every time actual list , database contain right value).

i cant understand problem why notifydatasetchanged() method behave like , solution it.

code inside baseadapter class:

public class customreminderadapter extends baseadapter {      private context context;     private list<reminderdata> datalist;     viewholder holder;     private addreminderhelperdao dao;       public customreminderadapter(context context,             list<reminderdata> datalist) {         super();         this.context = context;         this.datalist = datalist;      }      @override     public int getcount() {         return datalist.size();     }      @override     public object getitem(int position) {         return datalist.get(position);     }      @override     public long getitemid(int position) {         return datalist.get(position).getid();     }  @override public view getview(int position, view convertview, viewgroup parent) {      layoutinflater minflater = (layoutinflater) context.getsystemservice(activity.layout_inflater_service);      if (convertview == null) {         convertview = minflater.inflate( r.layout.custom_list_item, null);         holder = new viewholder();         holder.delete = (linearlayout) convertview .findviewbyid(r.id.deletelayout);          holder.delete.settag(position);          convertview.settag(holder);     } else {         holder = (viewholder) convertview.gettag();     }      holder.delete.setonclicklistener(new view.onclicklistener() {         @override         public void onclick(view v) {             // datalist contain values database             int positiontoremove = (int)v.gettag();             int id= datalist.get(positiontoremove).getid();              try {                 // dao method call delete database                 dao.deletereminder(id);                 datalist.remove(positiontoremove);                 notifydatasetchanged();             } catch (exception e) {                 e.printstacktrace();             } {                 dao.close();             }          }     }); } 

}

viewholder class:

private class viewholder {     private textview titletextview, timedetail;     private imageview remindertypeimageview,conformimage;     private imageview sendwish;     private linearlayout topsidelayout, bglayout, bottomsidelayout,             bottomrightstrip, toplayout, editlayout, snoozedlayout;     private linearlayout delete, conform, edit; } 

please me.

its difficult guessing without knowing full class file. however, should follow viewholder pattern , change getview method below :

@override     public view getview(int position, view convertview, viewgroup parent) {          layoutinflater minflater = (layoutinflater) context.getsystemservice(activity.layout_inflater_service);         final viewholder holder;         if (convertview == null) {             convertview = minflater.inflate(r.layout.custom_list_item, parent, false);             holder = new viewholder();              // initializa finviewbyids here               //set tag of position             holder.delete.settag(position);              // set holder convertview reusing it.             convertview.settag(holder);         } else {             holder = (viewholder) convertview.gettag();         }          //do business logic here         holder.delete.setonclicklistener(new view.onclicklistener() {             @override             public void onclick(view v) {                 // datalist contain values database                 int positiontoremove = (int) v.gettag();                 int id = datalist.get(positiontoremove).getid();                  try {                     // dao method call delete database                     dao.deletereminder(id);                     datalist.remove(positiontoremove);                     notifydatasetchanged();                 } catch (exception e) {                     e.printstacktrace();                 } {                     dao.close();                 }              }         });      } 

change

convertview = minflater.inflate( r.layout.custom_list_item, null); 

to

 convertview = minflater.inflate( r.layout.custom_list_item, parent,false); 

also, update getitemid()

 @override      public long getitemid(int position) {         return position;     }  

edit :

holder.delete.setonclicklistener(new view.onclicklistener() {         @override          public void onclick(view v) {             // datalist contain values database              int positiontoremove = (int)v.gettag();             int id= datalist.get(positiontoremove).getid();              try {                  // dao method call delete database                  dao.deletereminder(id);                 // directly pass object instead of passing positions                 datalist.remove(datalist.get(positiontoremove));                 notifydatasetchanged();             } catch (exception e) {                 e.printstacktrace();             } {                  dao.close();             }           }      });  

Comments

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -