Data varying in GridView in Android App -
i developing android app displays information of different historical places. in app, have made page review , rate place. app retrieving usernames, ratings, , comments database , displaying in gridview. displaying correct number of comments instead of displaying comments duplicates of comments. here code retrieve data database.
can tell problem code??
class task extends asynctask<string, string, void> { private progressdialog progressdialog = new progressdialog(comments.this); inputstream = null ; string result = ""; protected void onpreexecute() { if (get) progressdialog.setmessage("retrieving reviews..."); else progressdialog.setmessage("posting review..."); progressdialog.show(); progressdialog.setoncancellistener(new oncancellistener() { @override public void oncancel(dialoginterface arg0) { task.this.cancel(true); } }); } @override protected void doinbackground(string... params) { httpclient httpclient = new defaulthttpclient(); httppost httppost = new httppost(url_select); try { httppost.setentity(new urlencodedformentity(param)); httpresponse httpresponse = httpclient.execute(httppost); httpentity httpentity = httpresponse.getentity(); //read content = httpentity.getcontent(); } catch (exception e) { log.e("log_tag", "error in http connection "+e.tostring()); } try { bufferedreader br = new bufferedreader(new inputstreamreader(is)); stringbuilder sb = new stringbuilder(); string line = ""; while((line=br.readline())!=null) { sb.append(line+"\n"); } is.close(); result=sb.tostring(); } catch (exception e) { // todo: handle exception log.e("log_tag", "error converting result "+e.tostring()); } return null; } protected void onpostexecute(void v) { try { if(get) { name=new arraylist<string>(); comment=new arraylist<string>(); rating=new arraylist<float>(); jsonarray jarray = new jsonarray(result); for(int i=0;i<jarray.length();i++) { jsonobject jasonobject = new jsonobject(); jasonobject = jarray.getjsonobject(i); string names=null; names=jasonobject.getstring("name"); name.add(names); comment.add(jasonobject.getstring("comment")); rating.add((float)jasonobject.getdouble("rating")); } customgrid adapter = new customgrid(comments.this, name,comment,rating); grid.setadapter(adapter); get=false; } progressdialog.dismiss(); } catch (exception e) { // todo: handle exception log.e("log_tag", "error parsing data "+e.tostring()); } } } adapter code:
public class customgrid extends baseadapter { private context mcontext; private final arraylist<string> name; private final arraylist<string> comment; private final arraylist<float> rating; public customgrid(context c,arraylist<string> name, arraylist<string> comment, arraylist<float> rating ) { mcontext = c; this.name= name; this.comment = comment; this.rating=rating; } @override public int getcount() { // todo auto-generated method stub return comment.size(); } @override public object getitem(int position) { // todo auto-generated method stub return null; } @override public long getitemid(int position) { // todo auto-generated method stub return 0; } @override public view getview(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, null); textview textname = (textview) grid.findviewbyid(r.id.grid_name); textview textcomment = (textview) grid.findviewbyid(r.id.grid_comment); ratingbar ratingbar1 = (ratingbar)grid.findviewbyid(r.id.grid_rating); textname.settext(name.get(position)); textcomment.settext(comment.get(position)); ratingbar1.setrating(rating.get(position)); } else { grid = (view) convertview; } return grid; } }
convertview null once. after inflate , return it, due recycling mechanism of gridview/listview never null again. doing assign textviews content of dataset @ position 0, , return on of pooled view same content on , over. change getview like:
@override public view getview(int position, view convertview, viewgroup parent) { if (convertview == null) { layoutinflater inflater = (layoutinflater) mcontext.getsystemservice(context.layout_inflater_service); convertview = inflater.inflate(r.layout.grid, null); } textview textname = (textview) convertview.findviewbyid(r.id.grid_name); textview textcomment = (textview) convertview.findviewbyid(r.id.grid_comment); ratingbar ratingbar1 = (ratingbar)convertview.findviewbyid(r.id.grid_rating); textname.settext(name.get(position)); textcomment.settext(comment.get(position)); ratingbar1.setrating(rating.get(position)); return convertview; } also want in viewholder pattern, makes gridview/listview scroll smoother
Comments
Post a Comment