Android AutoCompleteTextView 不显示任何下拉项
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10753336/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
AutoCompleteTextView not showing any drop down items
提问by Housefly
My XML:
我的 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" />
MY java code:
我的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);
This is not working atall....i mean its just working like an EditTextView. Where am i wrong??
这根本不起作用......我的意思是它就像一个EditTextView一样工作。我哪里错了??
complete code:
完整代码:
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
}
}
回答by Awemo
I just saw your other question before seeing this one. I was struggling with autocomplete for some time and I almost reverted to your new implementation of downloading all the keywords until I finally got it to work. What I did was;
在看到这个问题之前,我刚刚看到了你的另一个问题。我在自动完成方面苦苦挣扎了一段时间,我几乎恢复了下载所有关键字的新实现,直到我终于让它工作。我所做的是;
//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);
In my textwatcher's onTextChanged method, I get the suggests using an asynctask
在我的 textwatcher 的 onTextChanged 方法中,我使用 asynctask 获得了建议
//suggestsThread is an AsyncTask object
suggestsThread.cancel(true);
suggestsThread = new WertAgentThread();
suggestsThread.execute(s.toString());
In the AsyncTask's onPostExecute I then update the autocompletetextview
在 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();
See setNotifyOnChangeand notifyDataSetChangedfor more information
有关更多信息,请参阅setNotifyOnChange和notifyDataSetChanged
回答by vikas kumar
this is a snippet from my project. I think after you got data from services all you have to do is to:
这是我项目的一个片段。我认为在您从服务中获取数据后,您所要做的就是:
- clear your previous data.
- clear the previous adapter values.
- then add values to your list of data using add() or addAll() method.
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();
}
- 清除之前的数据。
- 清除以前的适配器值。
- 然后使用 add() 或 addAll() 方法将值添加到您的数据列表中。
通过在适配器上调用 notifyDataSetChanged() 来通知更改的数据。
@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();
}
but before all this make sure you have attached the adapter to the auto complete textview if don't do it as follows:
但在这一切之前,请确保您已将适配器附加到自动完成文本视图,如果不这样做,如下所示:
ArrayAdapter<String> stringArrayAdapterPatient= new ArrayAdapter<String>(getActivity(),android.support.v7.appcompat.R.layout.select_dialog_item_material,stringArrayListPatients);
completeTextViewPatient.setAdapter(stringArrayAdapterPatient);
回答by Antonis Radz
The only working solution after updating adapter and notifying about changes instantly show dropDown
is reseting AutoCompleteTextView
text again, Kotlin example:
更新适配器并立即通知更改后唯一可行的解决方案dropDown
是AutoCompleteTextView
再次重置文本,Kotlin 示例:
with(autoCompleteTextView) {
text = text
// Place cursor to end
}
Java something like:
Java类似于:
autoCompleteTextView.setText(autoCompleteTextView.getText());
// Place cursor to end
回答by ??????? ?? ?????
AutoCompleteTextView.Invalidate() will do it.
AutoCompleteTextView.Invalidate() 会做到这一点。
回答by jigspatel
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);
its working just comment on et.addtext line...
它的工作只是评论 et.addtext 行...