i trying simple app populating spinner values (json value) got network call. not getting how associated json value, when item selected spinner. can 1 provide me article/reference on how this?
this json response
{ item: { name: "xyz", brands: [ { id: 123, name: "abc" } ] } }
this how parsed json.
public class user_activity extends actionbaractivity { requestqueue queue; arraylist<string> brand_list = new arraylist<string>(); spinner spinner; string brand_id; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_user_activity); this.spinner= (spinner) findviewbyid(r.id.spinner); loaduser(); } public void loaduser() { final progressdialog pdialog = new progressdialog(this); pdialog.setmessage("please wait.."); pdialog.show(); queue = volley.newrequestqueue(this); string url = "http://myapiurl"; jsonobjectrequest postrequest = new jsonobjectrequest(request.method.get, url, new response.listener<jsonobject>() { @override public void onresponse(jsonobject response) { pdialog.hide(); try { jsonarray array = response.getjsonobject("item").getjsonarray("brands"); (int i=0; i< array.length();i++) { string b = array.getjsonobject(i).getstring("name"); brand_list.add(b); } arrayadapter<string> adapter; adapter = new arrayadapter<string>(user_activity.this,android.r.layout.simple_dropdown_item_1line,brand_list); spinner.setadapter(adapter); spinner.setonitemselectedlistener(new adapterview.onitemselectedlistener() { @override public void onitemselected(adapterview<?> adapterview, view view, int i, long l) { toast.maketext(getbasecontext(),spinner.getselecteditem().tostring(),toast.length_long).show(); } @override public void onnothingselected(adapterview<?> adapterview) { } }); } catch (jsonexception e) { e.printstacktrace(); pdialog.hide(); } } }, new response.errorlistener() { @override public void onerrorresponse(volleyerror error) { // error log.d("error.response", string.valueof(error)); } } ); queue.add(postrequest); }
i want send associated id values i,e "123" next activity through button intent.
edit: modified code follows
but function set spinner returns empty
public class user_activity extends actionbaractivity { string phone_number; string access_token; button enterbutton; string brand_id; textview uname; requestqueue queue; arraylist<string> brand_list = new arraylist<string>(); arraylist<string> _ids = new arraylist<string>(); spinner spinner; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_user_activity); this.phone_number = getintent().getstringextra("phone"); this.access_token = getintent().getstringextra("token"); enterbutton = (button)findviewbyid(r.id.buttonenter); uname = (textview) findviewbyid(r.id.tvname); this.spinner= (spinner) findviewbyid(r.id.spinner); loaduser(phone_number, access_token); // nextactivity(b[0]); } public void loaduser(string phone, string token) { final progressdialog pdialog = new progressdialog(this); pdialog.setmessage("please wait.."); pdialog.show(); queue = volley.newrequestqueue(this); final string[] message = new string[1]; final string[] my_id = new string[1]; string url = "http://api_url"; jsonobjectrequest postrequest = new jsonobjectrequest(request.method.get, url, new response.listener<jsonobject>() { @override public void onresponse(jsonobject response) { pdialog.hide(); try { message[0] = response.getjsonobject("item").getstring("name"); uname.settext(message[0]); jsonarray array = response.getjsonobject("item").getjsonarray("brands"); (int i=0; i< array.length();i++) { string b = array.getjsonobject(i).getstring("name"); string id = array.getjsonobject(i).getstring("id"); _ids.add(id); brand_list.add(b); } string id = setspinner(); } catch (jsonexception e) { e.printstacktrace(); pdialog.hide(); } } }, new response.errorlistener() { @override public void onerrorresponse(volleyerror error) { // error log.d("error.response", string.valueof(error)); } } ); queue.add(postrequest); } public string setspinner(){ arrayadapter<string> adapter; final string[] id = new string[1]; adapter = new arrayadapter<string>(user_activity.this,android.r.layout.simple_dropdown_item_1line,brand_list); spinner.setadapter(adapter); spinner.setonitemselectedlistener(new adapterview.onitemselectedlistener() { @override public void onitemselected(adapterview<?> adapterview, view view, int i, long l) { id[0] = _ids.get(i); // toast.maketext(getbasecontext(), id[0] + ":" + spinner.getselecteditem().tostring(), toast.length_short).show(); } @override public void onnothingselected(adapterview<?> adapterview) { } }); toast.maketext(getbasecontext(), id[0].tostring() , toast.length_short).show(); return id[0]; }
there many ways.
one way create id
arraylist
arraylist<string> _ids = new arraylist<string>();
add values json
arrylist
string id = array.getjsonobject(i).getstring("id"); _ids.add(id);
and used in spinner.setonitemselectedlistener(....)
spinner.setonitemselectedlistener(new adapterview.onitemselectedlistener() { @override public void onitemselected(adapterview<?> adapterview, view view, int position, long l) { string id=_ids.get(position);//used id pass activity } .... });
Comments
Post a Comment