android - Update GridView in Asynctask doesn't work -
i have gridview, want update after 1 button clicked (i want change text , color of button after being clicked). try notifydatasetchanged(); inside onclick method nothing happened.
i use asynctask store data in sharedprefereces (in postexecute), part of data "brandname" of item/button have clicked. try update de view notifydatasetchanged(); in postexecute method not working.
i compare data in getview method change color of brandbutton stored in sharedprefereces , want refresh view if click in other button.
p.d. sorry poor english
this getview method
public view getview(final int position, view convertview, viewgroup parent) { // todo auto-generated method stub view grid; layoutinflater inflater = (layoutinflater) mcontext .getsystemservice(context.layout_inflater_service); if (convertview == null) { grid = new view(mcontext); grid = inflater.inflate(r.layout.grid_single, null); textview brandname = (textview) grid.findviewbyid(r.id.grid_text); imageview brandlogo = (imageview) grid.findviewbyid(r.id.grid_image); imageview twitterimg = (imageview) grid.findviewbyid(r.id.twitterimg); imageview facebookimg = (imageview) grid.findviewbyid(r.id.facebookimg); imageview instagramimg = (imageview) grid.findviewbyid(r.id.instagramimg); button selectbrand = (button) grid.findviewbyid(r.id.selectbrand); if(mbrandgenericdata.getdatalist().get(position).sociallist.size()>0){ for(int i=0; i<mbrandgenericdata.getdatalist().get(position).sociallist.size(); i++){ if(mbrandgenericdata.getdatalist().get(position).sociallist.get(i).socialtype.equalsignorecase("fb")){ facebookimg.setimagedrawable(facebookdrawable); } if(mbrandgenericdata.getdatalist().get(position).sociallist.get(i).socialtype.equalsignorecase("tw")){ twitterimg.setimagedrawable(twitterdrawable); } if(mbrandgenericdata.getdatalist().get(position).sociallist.get(i).socialtype.equalsignorecase("it")){ instagramimg.setimagedrawable(instagramdrawable); } } } new imageloadtask("http://api.socialmanageranalytics.com/"+mbrandgenericdata.getdatalist().get(position).logo, brandlogo).execute(); brandname.settext(mbrandgenericdata.getdatalist().get(position).name); //here change color , text of button if (brandname.gettext().equals(preferencemanager.getdefaultsharedpreferences(mcontext).getstring("brandname", ""))){ selectbrand.settext(r.string.selected); selectbrand.setbackgroundcolor(0xff18abd5); } selectbrand.setonclicklistener(new onclicklistener() { @override public void onclick(view arg0) { // todo auto-generated method stub new selectbrandtask("http://api.socialmanageranalytics.com/"+mbrandgenericdata.getdatalist().get(position).logo, mbrandgenericdata.getdatalist().get(position).name, mbrandgenericdata.getdatalist().get(position).id).execute(); customgrid.this.notifydatasetchanged(); } }); } else { grid = (view) convertview; } return grid; }
and asynctask
public class selectbrandtask extends asynctask<void, void, bitmap> { private string url; private string brandname; private string id; private progressdialog dialog; public selectbrandtask(string url, string brandname, string id) { this.url = url; this.brandname = brandname; this.id = id; } @override protected void onpreexecute() { super.onpreexecute(); dialog = progressdialog.show(mcontext, "seleccionando marca", "por favor espere..."); } @override protected bitmap doinbackground(void... params) { try { url urlconnection = new url(url); httpurlconnection connection = (httpurlconnection) urlconnection .openconnection(); connection.setdoinput(true); connection.connect(); inputstream input = connection.getinputstream(); bitmap mybitmap = bitmapfactory.decodestream(input); return mybitmap; } catch (exception e) { e.printstacktrace(); } return null; } @override protected void onpostexecute(bitmap result) { super.onpostexecute(result); sharedpreferences mypreference = preferencemanager.getdefaultsharedpreferences(mcontext); sharedpreferences.editor editor = mypreference.edit(); editor.putstring("brandname", brandname); editor.putstring("user", user); editor.putstring("brandimage", encodetobase64(result)); editor.putstring("brandid", id); editor.commit(); if (dialog != null) dialog.dismiss(); customgrid.this.notifydatasetchanged(); }
please me, in advance.
the problem you're setting values of views when convertview == null
, won't true in case after first few times getview()
called.
if move calls set text/colors of textview outside of block convertview
null, code should work once data has been downloaded.
since you'll have move textview's declaration outside of method, should use kind of viewholder make life easier , adapter more efficient. http://www.javacodegeeks.com/2013/09/android-viewholder-pattern-example.html decent job of explaining it, here's little bit related code:
public view getview(final int position, view convertview, viewgroup parent) { final brandsholder holder; if(convertview == null){ holder = new brandsholder(); convertview = inflater.inflate(r.layout.grid_single, parent, false); holder.brandname = (textview)convertview.findviewbyid(r.id.grid_text); .... // rest of item declarations convertview.settag(holder); } else holder = (brandsholder)convertview.gettag(); final brandgenericdata branddata = brandgenericdata.getdatalist().get(position); new imageloadtask("http://api.socialmanageranalytics.com/" + brand.logo, holder.brandlogo).execute(); holder.brandname.settext(brand.name); if (holder.brandname.gettext().equals(preferencemanager.getdefaultsharedpreferences(mcontext).getstring("brandname", ""))){ holder.selectbrand.settext(r.string.selected); holder.selectbrand.setbackgroundcolor(0xff18abd5); } ... // other adjustments want make each item } private class brandsholder { textview brandname; imageview brandlogo; button selectbrand; ... // rest of viewholder's variable declarations }
the code needs fill in rest, think should point in right direction.
Comments
Post a Comment