android - transfer data from listview with onclicklistener -
i'm trying create listview onclicklistener, when user click on item on list view start new activity.. how can transfer file listview activity? below code updating json data , updating listview
public void updatejsondata() { mcommentlist = new arraylist<hashmap<string, string>>(); jsonparser jparser = new jsonparser(); jsonobject json = jparser.getjsonfromurl(read_comments_url); try { mcomments = json.getjsonarray(tag_posts); // looping through posts according json object returned (int = 0; < mcomments.length(); i++) { jsonobject c = mcomments.getjsonobject(i); // gets content of each tag string title = c.getstring(tag_title); string content = c.getstring(tag_message); string username = c.getstring(tag_lname); string studnum = c.getstring(tag_username); // creating new hashmap /**sharedpreferences sp = preferencemanager.getdefaultsharedpreferences(bsitloungeactivity.this); string fname = sp.getstring("firstname","anon"); string lname = sp.getstring("lasstname","anon");**/ hashmap<string, string> map = new hashmap<string, string>(); map.put(tag_title, title); map.put(tag_message, content); map.put(tag_lname, username); map.put(tag_username, "id:"+studnum); // adding hashlist arraylist mcommentlist.add(map); // annndddd, our json data date same our array // listw } } catch (jsonexception e) { e.printstacktrace(); } } private void updatelist() { listadapter adapter = new simpleadapter(this, mcommentlist, r.layout.single_post, new string[] { tag_title, tag_message, tag_lname,tag_username}, new int[] { r.id.title, r.id.message, r.id.username,r.id.username2 }); listview lv = getlistview(); lv.setonitemclicklistener(new onitemclicklistener() { @override public void onitemclick(adapterview<?> parent, view view, int position, long id) { // method triggered if item click within our // list. our example won't using this, // useful know in real life applications. } }); } can use data inserted in hashmap? , how? please me anyone
use this:
@override public void onitemclick(adapterview<?> parent, view view, int position, long id) { intent intent = new intent(mainactivity.this,secondactivity.class); intent.putextra("key", mcommentlist.get(position).get(tag_username));// same others startactivity(intent); } }); and retrieve data in second activity on oncreate() method:
intent n = getintent(); string = n.getstringextra("key");
Comments
Post a Comment