获取当前建议从`AutoCompleteTextView` [英] Getting current suggestion from `AutoCompleteTextView`
问题描述
你如何在目前的顶级建议的 AutoCompleteTextView
?我有建议的项目,我有一个文本更改侦听器注册。本人也具有相同的屏幕上的列表。当他们打字,我想上滚动当前最好的建议。但我无法弄清楚如何访问当前的建议,或至少顶部建议。我想我在寻找类似 AutoCompleteTextView.getCurrentSuggestions()
:
autoCompleteTextView.addTextChangedListener(新TextWatcher(){
公共无效onTextChanged(CharSequence中,诠释开始,诠释之前,诠释计数){
串currentText = autoCompleteTextView.getText();
串bestGuess = autoCompleteTextView.getCurrentSuggestions()[0];
// ^^^ mewthod不存在
doSomethingWithGuess(bestGuess);
}
公共无效beforeTextChanged(CharSequence中,诠释开始,诠释计数,
之后INT){
// 没做什么
}
公共无效afterTextChanged(编辑S){
// 没做什么
}
});
我做你想要做以下code是什么:
@覆盖
保护无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.autocomplete_1);
适配器=新的ArrayAdapter<字符串>(这一点,
android.R.layout.simple_dropdown_item_1line,国家);
AutoCompleteTextView的TextView =(AutoCompleteTextView)findViewById(R.id.edit);
textView.setAdapter(适配器);
adapter.registerDataSetObserver(新DataSetObserver(){
@覆盖
公共无效调用onChanged(){
super.onChanged();
Log.d(TAG,数据集变);
对象项目= adapter.getItem(0);
Log.d(TAG,item.toString+ item.toString());
}
});
}
item.toString将打印上显示的第一个项目的文本。
请注意,这会发生,即使你不显示弹出式(建议)呢。此外,你应该检查是否存在通过筛选条件的任何物品(即用户输入)。
要解决的第一个问题:
INT dropDownAnchor = textView.getDropDownAnchor();
如果(dropDownAnchor == 0){
Log.d(TAG,下拉ID = 0); //不显示弹出
返回;
}
//做的东西
要解决的第二个问题,使用getCount将> 0
How do you get the current top suggestion in an AutoCompleteTextView
? I have it suggesting items, and I have a text change listener registered. I also have a list on the same screen. As they type, I want to scroll the list to the current "best" suggestion. But I can't figure out how to access the current suggestions, or at least the top suggestion. I guess I'm looking for something like AutoCompleteTextView.getCurrentSuggestions()
:
autoCompleteTextView.addTextChangedListener(new TextWatcher() {
public void onTextChanged(CharSequence s, int start, int before, int count) {
String currentText = autoCompleteTextView.getText();
String bestGuess = autoCompleteTextView.getCurrentSuggestions()[0];
// ^^^ mewthod doesn't exist
doSomethingWithGuess(bestGuess);
}
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// do nothing
}
public void afterTextChanged(Editable s) {
// do nothing
}
});
I've done what you want to do with the following code:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.autocomplete_1);
adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_dropdown_item_1line, COUNTRIES);
AutoCompleteTextView textView = (AutoCompleteTextView) findViewById(R.id.edit);
textView.setAdapter(adapter);
adapter.registerDataSetObserver(new DataSetObserver() {
@Override
public void onChanged() {
super.onChanged();
Log.d(TAG, "dataset changed");
Object item = adapter.getItem(0);
Log.d(TAG, "item.toString "+ item.toString());
}
});
}
item.toString will print the text that is displayed on the first item.
Note that this will happen even if you aren't showing the pop-up (suggestions) yet. Also, you should check if there are any items that passed the filter criteria (aka the user's input).
To solve the first problem:
int dropDownAnchor = textView.getDropDownAnchor();
if(dropDownAnchor==0) {
Log.d(TAG, "drop down id = 0"); // popup is not displayed
return;
}
//do stuff
To solve the second problem, use getCount > 0
这篇关于获取当前建议从`AutoCompleteTextView`的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!