java - How to update Litview item when Spinner seleted -
i have problem can not fix now. create apps, there 1 spinner , 1 listview, , use baseadapter
load information on listview
on spinner
.
and now, when seleted spinner
item , listview
item turn on image.
public class countries implements serializable{ public string nameconutry; public string moneycountry; public string flag; public countries(){} public countries(string namecountry, string moneycountry, string flag){ this.nameconutry = namecountry; this.moneycountry = moneycountry; this.flag = flag; } public void save(context p_context) { sharedpreferences w_sp = p_context.getsharedpreferences("countries", activity.mode_private); sharedpreferences.editor w_editor = w_sp.edit(); w_editor.putstring("ctry_cd", nameconutry); w_editor.putstring("curr_name", nameconutry); w_editor.commit(); } public static countries load(context p_context) { countries countries = null; sharedpreferences w_sp = p_context.getsharedpreferences("countries", activity.mode_private); if (w_sp != null) { countries = new countries(); countries.nameconutry = w_sp.getstring("ctry_cd", ""); countries.moneycountry = w_sp.getstring("curr_name", ""); } return countries; } public jsonobject tojsonobject() { jsonobject w_json = new jsonobject(); try { w_json.put("ctry_cd", nameconutry); w_json.put("curr_name", moneycountry); } catch (jsonexception e) { return null; } return w_json; } public static countries fromjsonobject(jsonobject p_json) { countries countries = null; try { string w_namecountry = p_json.getstring("ctry_cd"); string w_moneycountry = p_json.getstring("curr_name"); string w_flag = p_json.getstring("curr_cd"); countries = new countries(w_namecountry, w_moneycountry, w_flag); } catch (jsonexception e) { return null; } return countries; } public string getnameconutry() { return nameconutry; } public void setnameconutry(string nameconutry) { nameconutry = nameconutry; } public string getmoneycountry() { return moneycountry; } public void setmoneycountry(string moneycountry) { moneycountry = moneycountry; } public string getflag() { return flag; } public void setflag(string flag) { flag = flag; } public class countriesactivity extends appcompatactivity { private static final string tag_ctry_cd = "ctry_cd"; private static final string tag_curr_name = "curr_name"; private searchview searchview; spinner spinner; private arraylist<countries> arraycountry; private jsonarray jsonarray; private countriesadapter01 adapter; private listview listview; textview txtsearch, txtback; progressdialog pg_dialog; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.countries_activity); searchview = (searchview) findviewbyid(r.id.searchview); init(); } private void init(){ new loadinfo().execute(); searchview(); listview = (listview) findviewbyid(r.id.listview); spinner = (spinner) findviewbyid(r.id.splist); txtsearch = (textview) findviewbyid(r.id.txtsearch); txtback = (textview) findviewbyid(r.id.txt_back); spinner.setonitemselectedlistener(new adapterview.onitemselectedlistener() { @override public void onitemselected(adapterview<?> adapterview, view view, int i, long l) { string title = arraycountry.get(i).nameconutry; toast.maketext(getapplicationcontext(), "you have selected : " + title, toast.length_short).show(); txtsearch.settext(title); adapter.imgselected = new imageview(getapplicationcontext()); adapter.imgselected.setvisibility(view.visible); } @override public void onnothingselected(adapterview<?> adapterview) { } }); listview.setonitemclicklistener(new adapterview.onitemclicklistener() { @override public void onitemclick(adapterview<?> adapterview, view view, int i, long l) { toast.maketext(getapplicationcontext(), "you have selected : " + arraycountry.get(i).nameconutry, toast.length_short).show(); } }); txtback.setonclicklistener(new view.onclicklistener() { @override public void onclick(view view) { onbackpressed(); } }); } private class loadinfo extends asynctask<string, string, string> { @override protected void onpreexecute() { super.onpreexecute(); pg_dialog=new progressdialog(countriesactivity.this); pg_dialog.setmessage("loading...."); pg_dialog.setindeterminate(false); pg_dialog.setcancelable(false); //pg_dialog.show(); } @override protected string doinbackground(string... strings) { jsonarray = getjsondata("countries.json"); return string.valueof(jsonarray); } @override protected void onpostexecute(string result) { super.onpostexecute(result); //pg_dialog.dismiss(); arraycountry = new arraylist<>(); try{ jsonarray = new jsonarray(result); for(int = 0;i < jsonarray.length();i++){ jsonobject json_data = jsonarray.getjsonobject(i); countries countries = new countries(); countries.nameconutry = json_data.getstring(tag_ctry_cd); countries.moneycountry= json_data.getstring(tag_curr_name); arraycountry.add(countries); } adapter = new countriesadapter01(countriesactivity.this, arraycountry); adapter.notifydatasetchanged(); spinner.setadapter(adapter); listview.setadapter(adapter); }catch (exception e){ toast.maketext(countriesactivity.this, "can not load information", toast.length_long).show(); } } } private void searchview(){ searchview.setonquerytextlistener(new searchview.onquerytextlistener() { @override public boolean onquerytextsubmit(string s) { return true; } @override public boolean onquerytextchange(string newtext) { if ( textutils.isempty(newtext) ) { adapter.getfilter().filter(""); } else { adapter.getfilter().filter(newtext.tostring()); } return true; } }); } private jsonarray getjsondata(string filename){ jsonarray jsonarray=null; try { inputstream = getassets().open(filename); int size = is.available(); byte[] data = new byte[size]; is.read(data); is.close(); string json = new string(data, "utf-8"); jsonarray=new jsonarray(json); }catch (ioexception e){ e.printstacktrace(); }catch (jsonexception je){ je.printstacktrace(); } return jsonarray; } @override protected void onresume() { super.onresume(); //init(); } @override protected void onrestart() { super.onrestart(); } public class countriesadapter01 extends baseadapter implements filterable, spinneradapter { private static final string tag_flag = "flag"; private context mcontext; arraylist<countries> arraylist; arraylist<countries> arraylistsearch; public textview txtctry; public imageview imgselected; public countriesadapter01(context mcontext, arraylist<countries> arraylist) { this.mcontext = mcontext; this.arraylist = arraylist; } @override public view getview(final int position, view convertview, viewgroup parent) { final myviewholder holder = new myviewholder(); view itemview = convertview; if(itemview == null) { layoutinflater inflater = (layoutinflater) mcontext.getsystemservice(context.layout_inflater_service); itemview = inflater.inflate(r.layout.item_countries, parent, false); } txtctry = (textview)itemview.findviewbyid(r.id.txt_ctry_cd); holder.txtcurrname = (textview)itemview.findviewbyid(r.id.txt_curr_name); holder.imgselected = (imageview) itemview.findviewbyid(r.id.img_selected); holder.imgflag = (imageview) itemview.findviewbyid(r.id.img_flag); txtctry.settext(arraylist.get(position).nameconutry); holder.txtcurrname.settext(arraylist.get(position).moneycountry); new asynctask<string, string, bitmap>(){ @override protected bitmap doinbackground(string... strings) { inputstream inputstream = null; string[] images = new string[0]; try { images = mcontext.getassets().list(tag_flag); arraylist<string> listimages = new arraylist<string>(arrays.aslist(images)); inputstream = mcontext.getassets().open(tag_flag + "/" + listimages.get(position)); } catch (ioexception e) { e.printstacktrace(); } final bitmap drawable = bitmapfactory.decodestream(inputstream); return drawable; } @override protected void onpostexecute(bitmap bitmap) { super.onpostexecute(bitmap); holder.imgflag.setimagebitmap(bitmap); } }.execute(); itemview.settag(holder); return itemview; } @override public int getcount() { return arraylist.size(); } @override public object getitem(int position) { return arraylist.get(position); } @override public long getitemid(int position) { return position; } @override public filter getfilter() { return new filter() { @override protected filterresults performfiltering(charsequence constraint) { final filterresults oreturn = new filterresults(); final arraylist<countries> results = new arraylist<countries>(); if (arraylistsearch == null) arraylistsearch = arraylist; if (constraint != null){ if(arraylistsearch !=null & arraylistsearch.size()>0 ){ ( final countries g :arraylistsearch) { if (g.nameconutry.tolowercase().contains(constraint.tostring()))results.add(g); } } oreturn.values = results; } return oreturn; } @override protected void publishresults(charsequence charsequence, filterresults filterresults) { arraylist = (arraylist<countries>)filterresults.values; notifydatasetchanged(); } }; } @override public view getdropdownview(int position, view convertview, viewgroup parent) { textview txtdrop = new textview(mcontext); txtdrop.setpadding(16, 16, 16, 16); txtdrop.settextsize(18); txtdrop.setgravity(gravity.center_vertical); txtdrop.settext(arraylist.get(position).nameconutry); txtdrop.settextcolor(color.parsecolor("#000000")); return txtdrop; } static class myviewholder{ public imageview imgflag, imgselected; public textview txtcurrname;
note:i load json , image assets folder.
result
Comments
Post a Comment