android - how to load listview depending upon spinner data? -
here code.
mainactivity.java
protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_callplan); // lv = getlistview(); city = (spinner) findviewbyid(r.id.city_spinner); peoplelist = new arraylist<hashmap<string, string>>(); citylist = new arraylist<city>(); city.setonitemselectedlistener(this); new getcity().execute(); } private void populatespinner() { // txtcategory.settext(""); (int = 0; < citylist.size(); i++) { cities.add(citylist.get(i).getname()); } // creating adapter spinner arrayadapter<string> spinneradapter = new arrayadapter<string>(this, android.r.layout.simple_spinner_item, cities); // drop down layout style - list view radio button spinneradapter .setdropdownviewresource(android.r.layout.simple_spinner_dropdown_item); // attaching data adapter spinner city.setadapter(spinneradapter); } public void onitemselected(adapterview<?> parent, view view, int position, long id) { // todo auto-generated method stub // new getcategories().execute(); spinner city1 = (spinner) parent; if (city1.getid() == r.id.city_spinner) { cityselected = cities.get(position); toast.maketext(getapplicationcontext(), cityselected, toast.length_long).show(); new getdoctorsdetails().execute(); } else { toast.maketext(getapplicationcontext(), "please select city dropdown box", toast.length_long) .show(); } }
asynctask spinner data database..
private class getcity extends asynctask<void, void, void> { @override protected void onpreexecute() { super.onpreexecute(); pdialog = new progressdialog(callplanactivity.this); pdialog.setmessage("fetching cities.."); pdialog.setcancelable(false); pdialog.show(); } @override protected void doinbackground(void... arg0) { servicehandler jsonparser = new servicehandler(); string json = jsonparser.makeservicecall(url_city, servicehandler.get); log.e("response: ", "> " + json); if (json != null) { try { jsonobject jsonobj = new jsonobject(json); if (jsonobj != null) { jsonarray categories = jsonobj .getjsonarray("doctors_details"); (int = 0; < categories.length(); i++) { jsonobject catobj = (jsonobject) categories.get(i); city cat = new city(catobj.getstring("city")); citylist.add(cat); } } } catch (jsonexception e) { e.printstacktrace(); } } else { log.e("json data", "didn't receive data server!"); } return null; } @override protected void onpostexecute(void result) { super.onpostexecute(result); if (pdialog.isshowing()) pdialog.dismiss(); populatespinner(); } }
and asynctask load listview items database based on value selected in spinner.
class getdoctorsdetails extends asynctask<string, string, jsonobject> { jsonobject jsonobject; @override protected void onpreexecute() { super.onpreexecute(); } @override protected jsonobject doinbackground(string... params) { // todo auto-generated method stub string city_name = cityselected.tostring(); list<namevaluepair> params1 = new arraylist<namevaluepair>(); params1.add(new basicnamevaluepair("city", city_name)); jsonobject = jparser.makehttprequest(url_doctors, "post", params1); return jsonobject; } @override protected void onpostexecute(jsonobject json) { if (json != null) { try { doctors_info = json.getjsonarray(tag_docdetails); (int = 0; < doctors_info.length(); i++) { jsonobject c = doctors_info.getjsonobject(i); string doc_name = c.getstring(tag_doc_name); string qualification = c.getstring(tag_doc_quali); hashmap<string, string> map = new hashmap<string, string>(); map.put(tag_doc_name, doc_name); map.put(tag_doc_quali, qualification); peoplelist.add(map); } } catch (jsonexception e) { e.printstacktrace(); } runonuithread(new runnable() { @override public void run() { // todo auto-generated method stub listview = (listview) findviewbyid(r.id.listview); // pass results listviewadapter.java adapter = new listviewadapter(callplanactivity.this, peoplelist); // set adapter listview listview.setadapter(adapter); adapter.notifydatasetchanged(); listview.notify(); } }); } pdialog.dismiss(); } }
now spinner data loaded correctly , listview data loaded correctly problem when select different value spinner data in listview appended instead of updating. value both spinner data appended in 1 listview. want display different data each selection on spinner value.
thanks in advance.
in onpostexecute()
method of getdoctorsdetails
async task never clear peoplelist
array before appending new results in it. whole onpostexecute()
method executed on ui thread, there's no need of runonuithread
.
what i'd first move listview = (listview) findviewbyid(r.id.listview);
oncreate()
method avoid searching on every spinner change.
instead of using peoplelist
local array why don't add items directly adapter? start onpostexecute()
adapter.clear()
. add items directly adapter adapter.add()
(note - depending on how you've implemented adapter might need little changes add()
). lastly adapter.notifydatasetchanged()
once , you're done.
also removing need of peoplelist
code more readable.
Comments
Post a Comment