Dynamically updating an AutoCompleteTextView adapter

后端 未结 5 1559
执笔经年
执笔经年 2020-11-28 23:20

I want to periodically change the suggestions given by an AutoCompleteTextview by getting the list from a RESTful web service, and can\'t get it working smoothly. I set up a

5条回答
  •  抹茶落季
    2020-11-28 23:49

    This is how I update my AutoCompleteTextView:

    String[] data = terms.toArray(new String[terms.size()]);  // terms is a List
    ArrayAdapter adapter = new ArrayAdapter(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

    提交回复
    热议问题