android - putExtra doesn't seem to be working at all. Using ListView to get a string and using listView to display a string -
i programming messaging app , want add users in group. however, when list of users pops , select 1 list, doesn't pass string (the username) other activity. empty list.
here code: first activity = sending data (usernames list) through putextra()
public class listusersactivity extends activity { private string currentuserid; private arrayadapter<string> namesarrayadapter; private arraylist<string> names; private listview userslistview; private button logoutbutton; private progressdialog progressdialog; private broadcastreceiver receiver = null; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_list_users); parse.initialize(this, "embpz0spruv5xwdgi23innll1sghg0kznikzg6kl", "lpsu4uffpeqfxkqb1gflcij4kvg20llpgbonlise"); currentuserid = parseuser.getcurrentuser().getobjectid(); names = new arraylist<>(); parsequery<parseuser> query = parseuser.getquery(); query.wherenotequalto("objectid", currentuserid); query.findinbackground(new findcallback<parseuser>() { public void done(list<parseuser> userlist, com.parse.parseexception e) { if (e == null) { (int i=0; i<userlist.size(); i++) { names.add(userlist.get(i).getusername().tostring()); } userslistview = (listview)findviewbyid(r.id.userslistview); namesarrayadapter = new arrayadapter<string>(getapplicationcontext(), r.layout.user_list_item, names); userslistview.setadapter(namesarrayadapter); userslistview.setonitemclicklistener(new adapterview.onitemclicklistener() { @override public void onitemclick(adapterview<?> a, view v, int i, long l) { intent gobacktoaddpoolintent = new intent(listusersactivity.this, addnewpoolactivity.class); addnewpoolactivity checker = new addnewpoolactivity(); checker.checkifuserisselected(userslistview.getitematposition(i).tostring()); gobacktoaddpoolintent.putextra("username", userslistview.getitematposition(i).tostring()); startactivity(gobacktoaddpoolintent); } }); } else { toast.maketext(getapplicationcontext(), "error loading user list", toast.length_long).show(); } } }); }
second activity = receiving data putextra()
public class addnewpoolactivity extends activity { private static arraylist<string> addedusers; private arrayadapter <string> addeduseradapter; private boolean userisselected; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_add_new_pool); button addmembers = (button) findviewbyid(r.id.baddmembers); addedusers = new arraylist<>(); //addedusers.add("group members"); addmembers.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { intent showuserstoselect = new intent(addnewpoolactivity.this, listusersactivity.class); startactivity(showuserstoselect); } }); listview addeduserslist = (listview) findviewbyid(r.id.addeduserslistview); addeduseradapter = new arrayadapter<>(this, r.layout.user_list_item, addedusers); addeduserslist.setadapter(addeduseradapter); if(userisselected){ bundle extras = getintent().getextras(); addedusers.add(extras.getstring("username")); } } public void checkifuserisselected(string user){ if (user!=null){ userisselected = true; }else{ userisselected = false; } }
in addition @codemagic 's answer (since boolean value false, won't call statement adding new data). it's because of parse data "username" after setadapter of listview. setting data, , trying add new data parsed list. either need before setting data set adapter, or call addedusersadapter.notifydatasetchanged()
refresh listview's data set.
addeduseradapter = new arrayadapter<>(this, r.layout.user_list_item, addedusers); addeduserslist.setadapter(addeduseradapter); bundle extras = getintent().getextras(); // check if username has been sent activity. if(extras != null && extras.containskey("username")){ addedusers.add(extras.getstring("username")); // refresh data set addeduseradapter.notifydatasetchanged(); }
Comments
Post a Comment