listview - List items get mixed up after adding new items android -
i have listview displays list of items fetched server. have created alertdialog edittext takes user input , makes api call , user input gets added items server. again new updated items displayed in listview.
the issue initailly when list displayed displayes properly. when user input posted, displayes randomly , not @ end of list.
here code:-
builder.setpositivebutton("post", new dialoginterface.onclicklistener() { public void onclick(dialoginterface dialog, int id) { boolean worked = true; postedstring = input1.gettext().tostring(); if(postedstring .length()==0 || postedstring =="") { toast.maketext(newsdetails.this, "please enter comment.", toast.length_long).show(); input1.findfocus(); worked = false; } else if(worked && postedstring !="") { dialog.dismiss(); pd = new progressdialog(details.this); pd.setmessage("posting.."); pd.show(); pd.setcancelable(true); post(postedstring ); } } private void post(string postedstring) { // todo auto-generated method stub string postcommenturl = endpoints.posturl; new postcomment().execute(postcommenturl); string getrequestforcomments = myurl; new fetchitems().execute(getrequest); } }) private class postcomment extends asynctask<string, string, string> { @override protected string doinbackground(string... params) { // todo auto-generated method stub httpresponse response =null; string resultstring = ""; string myresponsebody = "" ; // creating http client httpclient httpclient = new defaulthttpclient(); // creating http post httppost request = new httppost(params[0]); list<namevaluepair> namevaluepair = new arraylist<namevaluepair>(8); namevaluepair.add(my params) try { request.setentity(new urlencodedformentity(namevaluepair)); response = httpclient.execute(request); if(response.getstatusline().getstatuscode()== 200) { httpentity entity = response.getentity(); if (entity != null) { inputstream inputstream = entity.getcontent(); myresponsebody = converttostring(inputstream); } } } catch(exception e) { e.printstacktrace(); } return myresponsebody; } private class fetchitems extends asynctask<string,string, void> { // todo auto-generated method stub @override protected void doinbackground(string... params) { // todo auto-generated method stub httpresponse response =null; string resultstring = ""; string myresponsebody = "" ; // creating http client httpclient httpclient = new defaulthttpclient(); // creating http post httpget request = new httpget(params[0]); try { response = httpclient.execute(request); if(response.getstatusline().getstatuscode()== 200) { httpentity entity = response.getentity(); if (entity != null) { inputstream inputstream = entity.getcontent(); myresponsebody = converttostring(inputstream); try { jsonobject jsonresponse = new jsonobject(myresponsebody); jsonarray mtusers = jsonresponse.getjsonarray("listofcomments"); (int = 0; < mtusers.length(); i++) { jsonobject menuobject = mtusers.getjsonobject(i); string authorname= parsed code map = new hashmap<string,string>(); map.put(my params) mylist.add(map); } } catch (jsonexception e) { // todo auto-generated catch block e.printstacktrace(); } collections.reverse(mylist); getlist = mylist; } } } catch(exception e) { } return null; } @override protected void onpostexecute(void result) { // todo auto-generated method stub super.onpostexecute(result); if(pd!=null && pd.isshowing()) pd.dismiss(); itemsadapter = (listadapter) new cadapter(details.this, getlist); mylist.setadapter(itemsadapter); listviewinscrollviewheight.setlistviewheightbasedonchildren(mylist); mylist.invalidate(); mylist.refreshdrawablestate(); mylist.post(new runnable() { @override public void run() { // todo auto-generated method stub mylist.setselection(itemsadapter.getcount()-1); } }); }
//adapter
public view getview(int position, view convertview, viewgroup parent) { view vi=convertview; lazyviewholder viewholder; if(convertview==null) { vi = inflater.inflate(com.zevenpooja.attini.r.layout.comments_row,null); viewholder = new lazyviewholder(); viewholder.title = (textview)vi.findviewbyid(r.id.title); // title viewholder.description = (textview)vi.findviewbyid(r.id.description); // artist name viewholder.pubdate = (textview)vi.findviewbyid(r.id.txtpubdate); vi.settag(viewholder); } else { viewholder = (lazyviewholder)vi.gettag(); } hashmap<string, string> song = new hashmap<string, string>(); song = data.get(position); // setting values in listview //set title string mytitle =song.get(details.authordisplayname); stringbuilder sb = new stringbuilder(mytitle); int = 0; while ((i = sb.indexof(" ", + 40)) != -1) { sb.replace(i, + 1, "\n"); } viewholder.title.settext(sb.tostring()); //set description string mydescription = song.get(details.text); viewholder.description.settext(mydescription); // set image // string bitmapurl = song.get(home.avatarurl); //set date string mydate = song.get(newsdetails.dateupdated); final string old_format = "yyyy-mm-dd't'hh:mm:ss'z'"; final string new_format = "hh:mm, mmmm dd, yyyy"; string newdate =""; simpledateformat sdf = new simpledateformat(old_format); try { date d = sdf.parse(mydate); sdf.applylocalizedpattern(new_format); newdate = sdf.format(d); viewholder.pubdate.settext(newdate); } catch (java.text.parseexception e) { // todo auto-generated catch block e.printstacktrace(); } return vi; }
did check returned jsonarray? contain items in right oder? if no, problem how server-side script queries database. example if querying mysql database may sort results id order id desc
.
Comments
Post a Comment