Android - AutoCompleteTextView only works when backspacing -
i have autocompletetextview
dynamically updates list of suggestions user types. problem type list gets updated drop-down isn't shown! when delete character (backspace) drop-down shows up!
i tried explicitly call autotext.showdropdown();
after text change doesn't work.
here textchangedlistener
:
private textwatcher textchecker = new textwatcher() { @override public void beforetextchanged(charsequence charsequence, int i, int i2, int i3) { } @override public void ontextchanged(charsequence charsequence, int i, int i2, int i3) { if(autotext.length()>9){ new geotask().execute(); } } @override public void aftertextchanged(editable editable) { if(autotext.length()>9){ autotext.showdropdown(); log.i("update","showdropdown"); } } };
i tried resetting adapter above autotext.showdropdown();
still nothing:
autotext.setadapter(null); autotext.setadapter(adapter);
is there way work?
edit: here complete code of activity. perhaps can find doing wrong...
public class testdoctor extends activity { textview lattext; textview lngtext; autocompletetextview autotext; arrayadapter<string> adapter; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.test_doctor); lattext = (textview) findviewbyid(r.id.lattextview); lngtext = (textview) findviewbyid(r.id.longtextview); autotext = (autocompletetextview) findviewbyid(r.id.autocompletetextview); button button = (button) findviewbyid(r.id.buttondoctor); button.setonclicklistener(new button.onclicklistener() { public void onclick(view v) { new geotask().execute(); } }); autotext.setthreshold(10); autotext.sethint("Οδός Αριθμός, Περιοχή"); adapter = new arrayadapter<string>(this, android.r.layout.simple_dropdown_item_1line); autotext.setadapter(adapter); adapter.setnotifyonchange(true); autotext.addtextchangedlistener(textchecker); } private textwatcher textchecker = new textwatcher() { @override public void beforetextchanged(charsequence charsequence, int i, int i2, int i3){} @override public void ontextchanged(charsequence charsequence, int i, int i2, int i3) { if(autotext.length()>9){ new geotask().execute(); } } @override public void aftertextchanged(editable editable) {} }; @override public boolean oncreateoptionsmenu(menu menu) { // inflate menu; adds items action bar if present. getmenuinflater().inflate(r.menu.main, menu); return true; } private list<simpleaddress> getaddressesfromtext(string address) { address = address.replace(' ', '+'); httphelper httphelper = new httphelper(); inputstream inputstream = httphelper.makehttpgetrequest("http://maps.google.com/maps/api/geocode/json?address=" + address + "&sensor=false"); string response = httphelper.inputstreamtostring(inputstream); list<simpleaddress> simpleaddresses = new arraylist<simpleaddress>(); try { jsonobject jsonobject = new jsonobject(response); int size = ((jsonarray) jsonobject.get("results")).length(); (int = 0; i<size; i++){ string formatted_address = ((jsonarray) jsonobject.get("results")).getjsonobject(i) .getstring("formatted_address"); double lng = ((jsonarray) jsonobject.get("results")).getjsonobject(i) .getjsonobject("geometry").getjsonobject("location") .getdouble("lng"); double lat = ((jsonarray) jsonobject.get("results")).getjsonobject(i) .getjsonobject("geometry").getjsonobject("location") .getdouble("lat"); simpleaddresses.add(i,new simpleaddress(formatted_address,lat,lng)); } return simpleaddresses; } catch (jsonexception e) { return null; } } public class geotask extends asynctask<void, void, void> { list<simpleaddress> simpleaddresses = new arraylist<simpleaddress>(); @override protected void doinbackground(void... voids) { log.i("update","1"); try { simpleaddresses = getaddressesfromtext(autotext.gettext().tostring()); } catch (nullpointerexception e) { } log.i("update","2"); return null; } @override protected void onpostexecute(void avoid) { log.i("update", "3"); int size = simpleaddresses.size(); if(size > 0){ adapter.clear(); log.i("adapter_size",""+size); (int = 0; i< size; i++){ adapter.add(simpleaddresses.get(i).getformatted_address()); log.i("added",simpleaddresses.get(i).getformatted_address()); } log.i("update","4"); autotext.setadapter(null); autotext.setadapter(adapter); autotext.showdropdown(); log.i("update","showdropdown"); } super.onpostexecute(avoid); } } }
edit 2 tried still doesn't work. dropdown shows when press backspace...
@override public void aftertextchanged(editable editable) { runonuithread(updateadapter); }
...
private runnable updateadapter = new runnable() { public void run(){ log.i("adapter_update","start"); adapter.notifydatasetchanged(); autotext.showdropdown(); log.i("adapter_update","end"); } };
note problem is: type address. type nothing shows up. see in logcat the adapter updated. dropdown doesn't show. when delete 1 character dropdown shows up??!!
seems need implement filter:
import java.io.inputstream; import java.util.arraylist; import java.util.list; import org.json.jsonarray; import org.json.jsonexception; import org.json.jsonobject; import android.app.activity; import android.os.asynctask; import android.os.bundle; import android.util.log; import android.view.menu; import android.view.view; import android.widget.arrayadapter; import android.widget.autocompletetextview; import android.widget.button; import android.widget.filter; import android.widget.textview; public class testdoctor extends activity { textview lattext; textview lngtext; autocompletetextview autotext; arrayadapter<string> adapter; private filter filter; private static final int address_treshold = 10; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.test_doctor); lattext = (textview) findviewbyid(r.id.lattextview); lngtext = (textview) findviewbyid(r.id.longtextview); autotext = (autocompletetextview) findviewbyid(r.id.autocompletetextview); button button = (button) findviewbyid(r.id.buttondoctor); button.setonclicklistener(new button.onclicklistener() { public void onclick(view v) { new adapterupdatertask().execute(); } }); autotext.setthreshold(address_treshold); autotext.sethint("Οδός Αριθμός, Περιοχή"); filter = new filter() { @override protected void publishresults(charsequence constraint, filterresults results) { } @override protected filterresults performfiltering(charsequence constraint) { log.i("filter", "filter:" + constraint + " thread: " + thread.currentthread()); if (constraint != null && constraint.length() > address_treshold) { log.i("filter", "doing search .."); new adapterupdatertask().execute(); } return null; } }; adapter = new arrayadapter<string>(this, android.r.layout.simple_dropdown_item_1line) { public android.widget.filter getfilter() { return filter; } }; autotext.setadapter(adapter); adapter.setnotifyonchange(false); } @override public boolean oncreateoptionsmenu(menu menu) { // inflate menu; adds items action bar if present. getmenuinflater().inflate(r.menu.main, menu); return true; } private list<simpleaddress> getaddressesfromtext(string address) { address = address.replace(' ', '+'); httphelper httphelper = new httphelper(); inputstream inputstream = httphelper .makehttpgetrequest("http://maps.google.com/maps/api/geocode/json?address=" + address + "&sensor=false"); string response = httphelper.inputstreamtostring(inputstream); list<simpleaddress> simpleaddresses = new arraylist<simpleaddress>(); try { jsonobject jsonobject = new jsonobject(response); int size = ((jsonarray) jsonobject.get("results")).length(); (int = 0; < size; i++) { string formatted_address = ((jsonarray) jsonobject.get("results")) .getjsonobject(i).getstring("formatted_address"); double lng = ((jsonarray) jsonobject.get("results")).getjsonobject(i) .getjsonobject("geometry").getjsonobject("location") .getdouble("lng"); double lat = ((jsonarray) jsonobject.get("results")).getjsonobject(i) .getjsonobject("geometry").getjsonobject("location") .getdouble("lat"); simpleaddresses.add(i, new simpleaddress(formatted_address, lat, lng)); } return simpleaddresses; } catch (jsonexception e) { return null; } } public class adapterupdatertask extends asynctask<void, void, void> { list<simpleaddress> simpleaddresses = new arraylist<simpleaddress>(); @override protected void doinbackground(void... voids) { log.i("update", "1"); try { simpleaddresses = getaddressesfromtext(autotext.gettext().tostring()); } catch (nullpointerexception e) { } log.i("update", "2"); return null; } @override protected void onpostexecute(void avoid) { log.i("update", "3"); int size = simpleaddresses.size(); if (size > 0) { adapter.clear(); log.i("adapter_size", "" + size); (int = 0; < size; i++) { adapter.add(simpleaddresses.get(i).getformatted_address()); log.i("added", simpleaddresses.get(i).getformatted_address()); } log.i("update", "4"); adapter.notifydatasetchanged(); autotext.showdropdown(); } super.onpostexecute(avoid); } } }
Comments
Post a Comment