AutoCompleteTextView没有显示下拉选项。

16

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

    }
}

完全不工作是什么意思?你输入了多少个字符来进行检查? - Kazekage Gaara
4
您使用了阈值2,因此在您键入2个字符后它将触发。 - Venky
1
是的,我一直在输入超过2个字符...但它没有显示任何下拉项。 - Housefly
我的完整Java代码有大约500行.. :( - Housefly
我有几乎相同的代码,但我使用了ArrayAdapter<String>而不是String[],它起作用了。 - Pouya Danesh
显示剩余10条评论
7个回答

21

在看到这篇文章之前,我刚好看了你发布的另一个问题。我曾经尝试过实现自动补全,当时我差点放弃并采用了你最新实现的下载所有关键字的方法,后来我终于把它搞定了。我的做法是:

//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以获取更多信息。


23
似乎你还没有完全理解 notifyDataSetChanged() 方法的概念。在你的 onPostExecute 方法中创建了一个新的 ArrayAdapter 实例,并将其设置为适配器。在你的示例中,调用 notifyDataSetChanged() 方法是无用的。 - X.X_Mass_Developer

3
使用adapter.notifyDataSetChanged()方法来通知列表中的更改,如果这不起作用,那么您可以手动显示DropDown,如autoCompleteTextView.showDropDown()

3

这是我的项目中的一部分。我认为在从服务中获取数据后,您需要做的就是:

  1. clear your previous data.
  2. clear the previous adapter values.
  3. then add values to your list of data using add() or addAll() method.
  4. notify the data changed by calling notifyDataSetChanged() on adapter.

    @Override
    public void onGetPatient(List<PatientSearchModel> patientSearchModelList) {
    
    //here we got the raw data traverse it to get the filtered names data for the suggestions
    
    stringArrayListPatients.clear();
    stringArrayAdapterPatient.clear();
    for (PatientSearchModel patientSearchModel:patientSearchModelList){
    
        if (patientSearchModel.getFullName()!=null){
    
            stringArrayListPatients.add(patientSearchModel.getFullName());
    
        }
    
    }
    
    //update the array adapter for patient search
    stringArrayAdapterPatient.addAll(stringArrayListPatients);
    stringArrayAdapterPatient.notifyDataSetChanged();
    

    }

但在此之前,请确保您已将适配器附加到自动完成文本视图,如果没有,请按以下方式执行:
ArrayAdapter<String> stringArrayAdapterPatient= new ArrayAdapter<String>(getActivity(),android.support.v7.appcompat.R.layout.select_dialog_item_material,stringArrayListPatients);

completeTextViewPatient.setAdapter(stringArrayAdapterPatient);

0

调用AutoCompleteTextView.Invalidate()即可。


0
如果有人正在使用自定义对象数组列表,并且遇到了这个问题,请检查您的模型类并查看是否已经在toString中覆盖了正确的变量。如果您还没有覆盖,请覆盖toString。
public class MyModalClass {
    public int id;
    public String path;

    @Override
    public String toString() { //include this in your model and return what you need in your drop down
        return path;
    }
}

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

它正在工作,只需在et.addtext行上进行注释即可...


版本对于AutocompleteTextView有影响吗?我不知道...我的API比较低...需要通过更改来检查。 - Housefly

0

更新适配器并立即通知更改后,唯一的有效解决方案是重置AutoCompleteTextView文本以再次显示dropDown,以下是Kotlin示例:

 with(autoCompleteTextView) {
       text = text
  // Place cursor to end   
}

Java类似于:

autoCompleteTextView.setText(autoCompleteTextView.getText());
// Place cursor to end  

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接