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) {}
}
);
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:
and in the publishResults add the result to the adapter like this:
also and importent is that you need to custom the adapter and overrirde getFilter with your own new custom Filter class like this:
hope it was helpful for someone..