Dynamically updating an AutoCompleteTextView adapter

后端 未结 5 1560
执笔经年
执笔经年 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-29 00:10

    I didn't have any luck using adapter.notifyDataSetChanged() when dynamically adding and changing the data in the adapter. In my situation, I was hitting an external api asynchronously and getting a list of completion data periodically.

    This code clears the adapter, and adds the new data as you'd expect. However, I had to call the getFilter().Filter method to force the data to show. Also, I had to explicitly filter based on the current text in the AutocompleteTextView because my api call was asynchronous.

    adapter.clear();
    for (Map map : completions) {
         adapter.add(map.get("name"));
    }
    
    //Force the adapter to filter itself, necessary to show new data.
    //Filter based on the current text because api call is asynchronous. 
    adapter.getFilter().filter(autocompleteTextView.getText(), null);
    

提交回复
热议问题