AutoCompleteTextView不显示任何下拉项

Hou*_*fly 8 android autocompletetextview

我的XML:

<AutoCompleteTextView
        android:id="@+id/searchAutoCompleteTextView_feed"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:clickable="true"
        android:completionThreshold="2"
        android:hint="@string/search" />
Run Code Online (Sandbox Code Playgroud)

我的java代码:

AutoCompleteTextView eT = (AutoCompleteTextView)findViewById(R.id.searchAutoCompleteTextView_feed);
eT.addTextChangedListener(this);
String[] sa = new String[]{"apple", "mango", "banana", "apple mango", "mango banana"};
ArrayAdapter<String> aAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_dropdown_item_1line, sa);
eT.setAdapter(aAdapter);
Run Code Online (Sandbox Code Playgroud)

这不起作用....我的意思是它只是像EditTextView一样工作.我哪里错了?

完整代码:

public class FeedListViewActivity extends ListActivity implements TextWatcher{


    private AutoCompleteTextView eT;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.feed);

        eT = (AutoCompleteTextView) findViewById(R.id.searchAutoCompleteTextView_feed);
        eT.addTextChangedListener(this);

                    Thread thread = new Thread(null, loadMoreListItems);
                    thread.start();
    }

    private Runnable returnRes = new Runnable() {
        public void run() {

            //code for other purposes
        }
    };

    private Runnable loadMoreListItems = new Runnable() {
        public void run() {
            getProductNames();

            // Done! now continue on the UI thread
            runOnUiThread(returnRes);
        }
    };

    protected void getProductNames() {

            String[] sa = new String[]{"apple", "mango", "banana", "apple mango", "mango banana"};

            ArrayAdapter<String> aAdapter = new ArrayAdapter<String>(getApplicationContext(),
                    android.R.layout.simple_dropdown_item_1line, sa);
            eT.setAdapter(aAdapter);

    }

    public void afterTextChanged(Editable s) {
        // TODO Auto-generated method stub

    }

    public void beforeTextChanged(CharSequence s, int start, int count,
            int after) {
        // TODO Auto-generated method stub

    }

    public void onTextChanged(CharSequence s, int start, int before, int count) {
        // TODO Auto-generated method stub

    }
}
Run Code Online (Sandbox Code Playgroud)

Awe*_*emo 13

在看到这个之前我刚看到你的另一个问题.我在自动完成方面苦苦挣扎了一段时间,我几乎恢复了下载所有关键字的新实现,直到我终于开始工作.我做的是;

//In the onCreate
//The suggestArray is just a static array with a few keywords
this.suggestAdapter = new ArrayAdapter<String>(this, this.suggestionsView, suggestArray);
//The setNotifyOnChange informs all views attached to the adapter to update themselves 
//if the adapter is changed
this.suggestAdapter.setNotifyOnChange(true);
Run Code Online (Sandbox Code Playgroud)

在我的textwatcher的onTextChanged方法中,我使用asynctask得到了建议

//suggestsThread is an AsyncTask object
suggestsThread.cancel(true);
suggestsThread = new WertAgentThread();
suggestsThread.execute(s.toString());
Run Code Online (Sandbox Code Playgroud)

在AsyncTask的onPostExecute中,然后我更新了autocompletetextview

//suggestions is the result of the http request with the suggestions
this.suggestAdapter = new ArrayAdapter<String>(this, R.layout.suggestions, suggestions);
this.suggestions.setAdapter(this.suggestAdapter);
//notifydatasetchanged forces the dropdown to be shown.
this.suggestAdapter.notifyDataSetChanged();
Run Code Online (Sandbox Code Playgroud)

有关更多信息,请参阅setNotifyOnChangenotifyDataSetChanged

  • 您似乎还没有完全理解notifyDataSetChanged()方法的概念.您正在onPostExecute方法中创建ArrayAdapter的新实例,并将其设置为适配器.在您的示例中,notifyDataSetChanged()方法调用是无用的. (16认同)