AutocompleteTextView未显示建议

时间:2018-05-07 05:08:37

标签: android autocompletetextview

我一直试图让autocompletetextview工作,但到目前为止还没有显示任何结果。检查了很多帖子,但无法让我的工作,我也尝试了每个教程。这是我的代码:

Thread t1 = new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    autoCompleteAdapter = filler.execute(autoCompleteData).get();

                    inputEditText.setThreshold(5);
                    inputEditText.setAdapter(autoCompleteAdapter);
                    inputEditText.setTokenizer(new MultiAutoCompleteTextView.CommaTokenizer());
                    autoCompleteAdapter.notifyDataSetChanged();
                    System.out.println("adapter obtained " + inputEditText.getAdapter());
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });

这是异步任务' filer'

private class AutoCompleteFiller extends AsyncTask<ArrayList<String>, String, ArrayAdapter<String>> {

    @Override
    protected ArrayAdapter<String> doInBackground(ArrayList<String>[] autoCompleteData) {
        BufferedReader reader = null;
        try {
            reader = new BufferedReader(
                    new InputStreamReader(getAssets().open("wordcount.txt")));

            // do reading, usually loop until end of file reading
            String mLine;
            while ((mLine = reader.readLine()) != null) {
                autoCompleteData[0].add(mLine);
            }
            String[] finalData = new String[autoCompleteData[0].size()];

            for (int i = 0; i < finalData.length; i++) {
                finalData[i] = autoCompleteData[0].get(i);
            }
            ArrayAdapter<String> adapter = new ArrayAdapter<String>(getApplicationContext(),
                    android.R.layout.simple_dropdown_item_1line, finalData);
            System.out.println(autoCompleteData[0].size());
            return adapter;

        } catch (IOException e) {
            //log the exception
            e.printStackTrace();
        } finally {
            if (reader != null) {
                try {
                    reader.close();


                } catch (IOException e) {
                    //log the exception
                    e.printStackTrace();
                }
            }
        }
        return null;
    }
}

这是启动线程的代码:

t1.start();

谢谢。

1 个答案:

答案 0 :(得分:0)

我在设置适配器的部分以及从异步任务获取适配器之后的所有部分都需要在主线程上执行。答案由ADM在评论中发布。