Dynamically Updating an Autocompletetextview Adapter

Dynamically updating an AutoCompleteTextView adapter

This is how I update my AutoCompleteTextView:

String[] data = terms.toArray(new String[terms.size()]);  // terms is a List<String>
ArrayAdapter<?> adapter = new ArrayAdapter<Object>(activity, android.R.layout.simple_dropdown_item_1line, data);
keywordField.setAdapter(adapter); // keywordField is a AutoCompleteTextView
if(terms.size() < 40) keywordField.setThreshold(1);
else keywordField.setThreshold(2);

Now of course, this is static and doesn't deal with an over-the-air suggestions but, I can also suggest you to notify adapter for the changes after you assign it to the AutoCompleteTextView:

adapter.notifyDataSetChanged();   

Hope this helps.

-serkan

Refresh ArrayAdapter dynamically

Have you tried the setNotifyOnChange(true)? It is supposed to properly do what you are doing manually whenever you use methods that change the list (add(T), insert(T, int), remove(T), clear()). Perhaps you have to modify the array through these methods on the ArrayAdapter?

I'm not sure if the ArrayAdapter is actually holding the reference to schools or just copied the contents while constructing the ArrayAdapter. Maybe you can take a look at the AOSP code to see what they're doing in that constructor.

How to dynamically update drop down list on AutoCompleteTextView TextChangedListener?

Remove your code from onTextChanged(), and add this

  private Timer mTimer =new Timer();
private final long DELAY = 1500;

@Override
public void afterTextChanged(final Editable editable) {

mTimer.cancel();
if (!editable.toString().equals("")) {
mTimer = new Timer();
mTimer.schedule(
new TimerTask() {
@Override
public void run() {
runOnUiThread(new TimerTask() {
@Override
public void run() {
getStateList(editable.toString());
}
});

}
},
DELAY
);
}

}

In the response block add these lines, Clear your mList first.

@Override
public void onResponseListner(String response) {

        Log.i("EPF STATES:--", response.toString());
try {
mList.clear();
mList = new ArrayList<>();

JSONArray jsonArray = new JSONArray(response);
for (int j = 0; j < jsonArray.length(); j++) {
JSONObject obj = jsonArray.getJSONObject(j);
StateModel model = new StateModel();
model.setName(obj.getString("name"));
model.setId(obj.getString("id"));
mList.add(model);
}
mAdapter = new AreaAdapter(getActivity(),
android.R.layout.simple_spinner_item, mList,
true);
mAutoCompleteTextView.setAdapter(mAdapter);
mAutoCompleteTextView.showDropDown();
} catch (JSONException e) {
e.printStackTrace();
}
}

Xamarin.Android: Dynamic AutoCompleteTextView

According to your description, you want to add dynamic List for AutoCompleteTextView, I create one simple sample that you can take a lok:

public class MainActivity : AppCompatActivity
{
List<string> countries;
ArrayAdapter adapter;
AutoCompleteTextView textView;
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
Xamarin.Essentials.Platform.Init(this, savedInstanceState);
// Set our view from the "main" layout resource
SetContentView(Resource.Layout.activity_main);

countries = new List<string>() {
"Afghanistan","Albania","Algeria","American Samoa","Andorra",
"Vanuatu","Vatican City","Venezuela","Vietnam","Wallis and Futuna","Western Sahara",
"Yemen","Yugoslavia","Zambia","Zimbabwe"
};
textView = FindViewById<AutoCompleteTextView>(Resource.Id.autocomplete_country);
adapter = new ArrayAdapter(this, Resource.Layout.list_item, countries) ;

textView.Adapter = adapter;
Button btnadd = FindViewById<Button>(Resource.Id.button1);
btnadd.Click += Btnadd_Click;
textView.Adapter = adapter;
}

private void Btnadd_Click(object sender, EventArgs e)
{
countries.Clear();

countries = new List<string>()
{
"chinese","test","english"
};
adapter.AddAll(countries);
adapter.NotifyDataSetChanged();


}


Related Topics



Leave a reply



Submit