android - Like button and like counter resets to old value after scrolling up and down in listview -


after liking news item heart button shows red , counter sets 1 after scrolling , down counter goes 0 , heart button changes red ash loaded server. tried using notifydatasetchanged() didn't work in adapter ....below code implemented.

public class listviewadaptersup extends baseadapter {  // declare variables context context; layoutinflater inflater; arraylist<hashmap<string, string>> data; imageloader imageloader; hashmap<string, string> resultp = new hashmap<string, string>();    public listviewadaptersup(context context,         arraylist<hashmap<string, string>> arraylist) {     this.context = context;     data = arraylist;     imageloader = new imageloader(context); }  @override public int getcount() {     return data.size(); }    @override public object getitem(int position) {     return null; }  @override public long getitemid(int position) {     return 0; }  static class viewholder {      // declare variables     textview supmessagetv;     imageview imagelike;     imageview imagecomment; }  viewholder holder;    public view getview(final int position, view itemview, viewgroup parent)     {     itemview = null;      if (itemview == null) {          //itemview.settag();     inflater = (layoutinflater) context.getsystemservice(context.layout_inflater_service);      itemview = inflater.inflate(r.layout.sups_list_item, parent, false);     // position     resultp = data.get(position);     holder = new viewholder();      // locate textviews in listview_item.xml     holder.messagetv = (textview) itemview.findviewbyid(r.id.status);     // locate imageview in listview_item.xml     holder.imagelike = (imageview) itemview.findviewbyid(r.id.likeimage);     holder.imagecomment = (imageview) itemview.findviewbyid(r.id.imagecomment);      holder.viewlikesimageclick = (imageview) itemview.findviewbyid(r.id.imagevieew);      }       else{           holder = (viewholder) itemview.gettag();      }         // capture position , set results textviews     holder.messagetv.settext(resultp.get(news.message));     holder.likescounttv.settext(resultp.get(news.likes_count));     holder.commentscounttv.settext(resultp.get(news.comments_count));                holder.imagelike.settag(holder);             holder.imagelike.setonclicklistener(new view.onclicklistener() {                  @targetapi(build.version_codes.lollipop)                 @override                 public void onclick(view view) {                       if (build.version.sdk_int < build.version_codes.lollipop) {                           holder = (viewholder)view.gettag();                             resultp = data.get(position);                              if(holder.imagelike.getdrawable().getconstantstate().equals(context.getresources().getdrawable(r.drawable.ic_favorite_white_24dp).getconstantstate())){                             resultp = data.get(position);                                  connectiondetector cd = new connectiondetector(context);                                 if (!cd.isconnectingtointernet()) {                                      toast.maketext(context,"no internet connection",toast.length_short).show();                                       }                                 else                                 {                                  new likenews().execute();                                 string number = holder.likescounttv.gettext().tostring();                                 int num = integer.parseint(number);                                 int nene = num + 1;                                 string str = string.valueof(nene);                                  holder.likescounttv.settext(str);                                     //holder.likebtn.settext("liked");                                 holder.imagelike.setimageresource(r.drawable.ic_favorite_red_24dp);                                  }                          }                              else if(holder.imagelike.getdrawable().getconstantstate().equals(context.getresources().getdrawable(r.drawable.ic_favorite_red_24dp).getconstantstate())){                                 resultp = data.get(position);                                      connectiondetector cd = new connectiondetector(context);                                     if (!cd.isconnectingtointernet()) {                                          toast.maketext(context,"no internet connection",toast.length_short).show();                                           }                                     else                                     {                                      new unlikenews().execute();                                     string number = holder.likescounttv.gettext().tostring();                                     int num = integer.parseint(number);                                     int nene = num - 1;                                     string str = string.valueof(nene);                                      holder.likescounttv.settext(str);                                       //  holder.likebtn.settext("like");                                     holder.imagelike.setimageresource(r.drawable.ic_favorite_white_24dp);                                      }                                 }                          }                             else if (build.version.sdk_int >= build.version_codes.lollipop) {                                   holder = (viewholder)view.gettag();                                 resultp = data.get(position);                                  if(holder.imagelike.getdrawable().getconstantstate().equals(holder.imagelike.getcontext().getdrawable(r.drawable.ic_favorite_white_24dp).getconstantstate())){                                 resultp = data.get(position);                                      connectiondetector cd = new connectiondetector(context);                                     if (!cd.isconnectingtointernet()) {                                          toast.maketext(context,"no internet connection",toast.length_short).show();                                           }                                     else                                     {                                      new likenews().execute();                                     string number = holder.likescounttv.gettext().tostring();                                     int num = integer.parseint(number);                                     int nene = num + 1;                                     string str = string.valueof(nene);                                      holder.likescounttv.settext(str);                                         holder.imagelike.setimageresource(r.drawable.ic_favorite_red_24dp);                                      }                              }                                  else if(holder.imagelike.getdrawable().getconstantstate().equals(holder.imagelike.getcontext().getdrawable(r.drawable.ic_favorite_red_24dp).getconstantstate())){                                     resultp = data.get(position);                                          connectiondetector cd = new connectiondetector(context);                                         if (!cd.isconnectingtointernet()) {                                              toast.maketext(context,"no internet connection",toast.length_short).show();                                               }                                         else                                         {                                          new unlikenews().execute();                                         string number = holder.likescounttv.gettext().tostring();                                         int num = integer.parseint(number);                                         int nene = num - 1;                                         string str = string.valueof(nene);                                          holder.likescounttv.settext(str);                                             holder.imagelike.setimageresource(r.drawable.ic_favorite_white_24dp);                                          }                                     }                              }                    }             });       return itemview; }   public class likenews extends asynctask<string, string, string> {      boolean hasuserliked = false;       protected void onpreexecute() {         super.onpreexecute();      }      @override     protected string doinbackground(string... strings) {              httpclient httpclient = new defaulthttpclient();             httppost httppost = new httppost("www.example.com");               try {                 list<namevaluepair> namevaluepairs = new arraylist<namevaluepair>(2);                     namevaluepairs.add(new basicnamevaluepair("id", "01"));                    namevaluepairs.add(new basicnamevaluepair("username", user));                    namevaluepairs.add(new basicnamevaluepair("user_involved", "mark");                     httppost.setentity(new urlencodedformentity(namevaluepairs));                    httpclient.execute(httppost);                     hasuserliked = true;           } catch (nullpointerexception e) {             e.printstacktrace();         } catch (exception e) {             e.printstacktrace();         }         return null;     }      @override     protected void onpostexecute(string lenghtoffile) {        }  }   public class unlikenews extends asynctask<string, string, string> {  protected void onpreexecute() {     super.onpreexecute();  }  @override protected string doinbackground(string... strings) {          httpclient httpclient = new defaulthttpclient();         httppost httppost = new httppost("www.example.com");           try {             list<namevaluepair> namevaluepairs = new arraylist<namevaluepair>(2);                 namevaluepairs.add(new basicnamevaluepair("id", "01"));                namevaluepairs.add(new basicnamevaluepair("username", user));                 httppost.setentity(new urlencodedformentity(namevaluepairs));                httpclient.execute(httppost);        } catch (nullpointerexception e) {         e.printstacktrace();     } catch (exception e) {         e.printstacktrace();     }     return null; }  @override protected void onpostexecute(string lenghtoffile) {  //          }   }      } 

resultp = data.get(position); should outside of if else loop.


Comments

Popular posts from this blog

sql - invalid in the select list because it is not contained in either an aggregate function -

Angularjs unit testing - ng-disabled not working when adding text to textarea -

How to start daemon on android by adb -