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
How to Create Topic in Fcm Notifications
How to Change the Track Color of a Switchcompat
Mediastore - Uri to Query All Types of Files (Media and Non-Media)
Android Gridview Draw Dividers
How to Open Private Files Saved to the Internal Storage Using Intent.Action_View
Use Roboto Font in App with Minimum API Level 14
Preferencefragmentcompat Has Padding on Preferencecategory That I Can't Get Rid Of
Eclipse Android Project, How to Reference Library Within Workspace
Cache.Properties (The System Cannot Find the File Specified)
How to Declare Git Repository as Dependency in Android Gradle
How to Keep Android Applications Always Be Logged in State
Android Location Manager, Get Gps Location ,If No Gps Then Get to Network Provider Location
Parcelable Encountered Ioexception Writing Serializable Object Getactivity()
Dialogfragment with Clear Background (Not Dimmed)