AutoCompleteTextView not responding to changes to its ArrayAdapter

前端 未结 1 2022
情深已故
情深已故 2021-01-07 04: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. I have tried addin

相关标签:
1条回答
  • 2021-01-07 04:57

    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.

    0 讨论(0)
提交回复
热议问题