Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - AutoCompleteTextView dynamic updating issue

well i'm trying to update the autoCompleteTextview ArrayAdapter dynamiclly each time text changed by using TextWathcer.

every time text changed there is an http request in a new AsyncTask and in the callback from the async task (onPostExecute) i call clear() from the adapter and adding new items to him and then call notifyDataSetChanged. Unfortunately the auto complete drop down is never shown..

please, where do i go worng?!

here is the code:

AutoCompleteTextView acCountry = (AutoCompleteTextView)layout.findViewById(R.id.autoComplete);
final ArrayAdapter<RMAutoComplete.ListItem> countriesAdapter = new ArrayAdapter<RMAutoComplete.ListItem>(this.context,android.R.layout.simple_dropdown_item_1line);
acCountry.addTextChangedListener(new TextWatcher() {

            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (s.length() > 1)
                {
                    new AsyncTask<String, Void, List<RMAutoComplete.ListItem>>(){

                        @Override
                        protected List<ListItem> doInBackground(String... params) {
                            List<ListItem> l = null;
                            try {
                                l = location.getCountryData(params[0]);
                            } catch (Exception e) {
                                Log.e(TAG,"error when getCountryData",e);
                            }
                            return l;
                        }

                        @Override
                        protected void onPostExecute(List<ListItem> countries) {
                            countriesAdapter.clear();
                            if (countries != null)
                                for (ListItem listItem : countries) {
                                    countriesAdapter.add(listItem);
                                }
                            countriesAdapter.notifyDataSetChanged();
                        }
                    }.execute(s.toString());
                }
            }

            public void beforeTextChanged(CharSequence s, int start, int count, int after) {}

            public void afterTextChanged(Editable s) {}
        }
    );
like image 972
Adler Avatar asked Apr 07 '26 06:04

Adler


1 Answers

Common mistake : Did you set the adapter to acCountry at all?

like image 108
rDroid Avatar answered Apr 08 '26 20:04

rDroid



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!