android - AutoCompleteTextView 不显示任何下拉项

标签 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" />

我的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);

这根本不起作用....我的意思是它只是像 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

    }
}

最佳答案

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

//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);

在我的 textwatcher 的 onTextChanged 方法中,我得到了使用异步任务的建议

//suggestsThread is an AsyncTask object
suggestsThread.cancel(true);
suggestsThread = new WertAgentThread();
suggestsThread.execute(s.toString());

然后在 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();

参见 setNotifyOnChangenotifyDataSetChanged了解更多信息

关于android - AutoCompleteTextView 不显示任何下拉项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10753336/

相关文章:

java - 如何在用户单击 autocom 时显示 HashMap 键的值

android - 如何在命令行上安装 Android SDK Build Tools?

Android:如何将 website.type_XXX 转换为字符串?

android - LolliPop 设备中的 AutoCompleteTextView 奇怪行为

android - AutoCompleteTextView 右侧的加载指示器

android - AutoCompleteTextView 不显示文本

java - 我无法将一个数组列表添加到另一个数组列表中

android - 视频 View setVideoUri 在 Android 中失败

android - “ViewModelProviders”已弃用。 Java解决方案?

Flutter:自动完成文本字段不适用于自定义数据类型