AutoCompleteTextView not responding to changes to its ArrayAdapter

我的未来我决定 提交于 2019-12-01 01:39:46

The ArrayList appears to be populating just fine, but no matter what approach I use, I can't seem to get the adapter to populate with data

You're going against how the AutoCompleTextView works. When the user starts entering characters in the input box the AutoCompleteTextView will filter the adapter and when that happens it will show the drop down with the values which managed to pass the filter request. Now you've setup the AutoCompleteTextView to make a thread each time the user enters a character, the problem is that the AutoCompleteTextview will never see those values as it requests the adapter to filter before the adapter actually gets populated with the right values. try something like this:

public static class BlockingAutoCompleteTextView extends
        AutoCompleteTextView {

    public BlockingAutoCompleteTextView(Context context) {
        super(context);
    }

    @Override
    protected void performFiltering(CharSequence text, int keyCode) {           
        // nothing, block the default auto complete behavior
    }

}

and to get the data:

public void onTextChanged(CharSequence s, int start, int before, int count) {
    if (autoComplete.getThreashold() < s.length()) {
        return;
    } 
    queryWebService();
}

You need to update the data on the main UI thread and also using the adapter's methods:

// do the http requests you have in the queryWebService method and when it's time to update the data:
runOnUiThread(new Runnable() {
        @Override
    public void run() {
        autoCompleteAdapter.clear();
        // add the data
                for (int i = 0; i < length; i++) {
                     // do json stuff and add the data
                     autoCompleteAdapter.add(theNewItem);                    
                } 
                // trigger a filter on the AutoCompleteTextView to show the popup with the results 
                autoCompleteAdapter.getFilter().filter(s, autoComplete);
    }
});

See if the code above works.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!