Q:

Dynamic updating AutoCompleteTextView popup the drop down again after item selected

I'm updating the AutoCompleteTextView dynamically and I'm facing two problems.

when item selected there is a new onTextChanged event and as you can see in the code below there is a new request to get a new optional items so the onTextChanged event cause the AutoComplete show the drop down again! is there a clean way to solve it?!
the results i get from the auto complete are from the previous adapter before I call notifyDataSetChange(), how can i make it happen?!

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.setAdapter(countriesAdapter);
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) {}
    }
);

A:

Well to achive the dynamic adapter what is need to do is to implement custom Filter class and override the performFiltering and publishResults methods.
In the performFiltering create new instance of FilterResults and initialize his values and count with the new items.. (this method works by default on a new thread!) something like this:
@Override
    protected FilterResults performFiltering(CharSequence constraint) {
        FilterResults fr = null;
        if (constraint != null)
        {
            List<ListItem> list = getFilterdValues(constraint.toString());

            if (list != null)
            {
                fr = new FilterResults();
                fr.values = list;
                fr.count = list.size();
            }
        }
        return fr;
    }

and in the publishResults add the result to the adapter like this:
@Override
    protected void publishResults(CharSequence constraint,
            FilterResults results) {
        AutoCompleteAdapter.this.clear();
        if (results != null)
        {
            if (results.values != null)
            {
                List<ListItem> items = (List<ListItem>) results.values;

                    for (ListItem listItem : items) {
                        AutoCompleteAdapter.this.add(listItem);
                    }

                if (items.size() > 0)
                {
                    AutoCompleteAdapter.this.notifyDataSetChanged();
                    return;
                }
            }
        }
        AutoCompleteAdapter.this.notifyDataSetInvalidated();
    }

also and importent is that you need to custom the adapter and overrirde getFilter with your own new custom Filter class like this:
public class AutoCompleteAdapter extends ArrayAdapter<RMAutoComplete.ListItem> 
{

private CustomFilter _customFilter = null;

@Override
public Filter getFilter() {

    if (_customFilter == null)
        _customFilter = new CustomFilter();
    return _customFilter;
}

public AutoCompleteAdapter(Context context, int textViewResourceId) {
    super(context, textViewResourceId);

}

}

hope it was helpful for someone..

