java - Getting HashMap associated with particular item in ListView -


i have listview showing items in arraylist fetched url in json format. each item consists of 5 elements. want pass elements associated particular list item when clicked activity. when pass arraylist other activity , iterate arraylist last item in list opens no matter list item click on.

here code:

mainactivity.java

jsonarray jobject = new jsonarray(responsebody); (int = 0; < jobject.length(); i++) {   jsonobject menuobject = jobject.getjsonobject(i);   string title= menuobject.getstring("title");   string description= menuobject.getstring("bodytext");   string thumbnail= menuobject.getstring("thumbnailpath");   string nurl = menuobject.getstring("url");   string body = menuobject.getstring("body");   string nbigimage = menuobject.getstring("imagebloburls");    map = new hashmap<string,string>();   map.put(sourcetitle, title);   map.put(title, description);   map.put(thumbnailpath, thumbnail);   map.put(body, body);   map.put(url, nurl);   map.put(imagebloburls,nbigimage);    mylist.add(map); }  mylistview.setadapter(itemsadapter); mylistview.setonitemclicklistener(new onitemclicklistener() {      @override     public void onitemclick(adapterview<?> arg0, view arg1, int position, long arg3)      {         // todo auto-generated method stub         intent nintent = new intent(home.this,ndetails.class);         nintent.putextra("myitems", mylist);         startactivity(nintent);     } }); 

ndetails.java

intent in = getintent();      data =  (arraylist<hashmap<string, string>>) in.getserializableextra("myitems");      for(map<string, string> newitem:data)     {          newurl = newitem.get(home.url);          newtitle =newitem.get(home.sourcetitle);          newbody= newitem.get(home.body);          newimage = newitem.get(home.thumbnailpath);      }        txtnewstitle.settext(newtitle);      txtnewsbody.settext(newbody); 

how resolve this? thanks

try :

mylistview.setonitemclicklistener(new onitemclicklistener()                          {                              @override                             public void onitemclick(adapterview<?> arg0,                                     view arg1, int position, long arg3)                              {                                 // todo auto-generated method stub                                  hashmap mymap = mylist.get(position);                                 intent nintent = new intent(home.this,ndetails.class);                                 newsintent.putextra("myitems", mymap);                                 startactivity(nintent);                             }                         }); 

and map in activity :

data =  (hashmap<string, string>>) in.getserializableextra("myitems");  newurl = data.get(home.url);      data =newitem.get(home.sourcetitle);      data= newitem.get(home.body);      data = newitem.get(home.thumbnailpath); 

Comments

Popular posts from this blog

javascript - Count length of each class -

What design pattern is this code in Javascript? -

hadoop - Restrict secondarynamenode to be installed and run on any other node in the cluster -