【发布时间】:2011-08-12 03:08:39
【问题描述】:
我在这里遇到了一些技术问题,例如,我有一个用于自动完成文本框的适配器,其中包含此列表 [巴林、白俄罗斯、巴哈马]。如果我输入例如“ah”,我想在 autocomplete_textbox 的下拉列表中显示这两个 [Bahrain, Bahamas],因为这两个包含“ah”的子字符串(B“ah”amas 和 B“ah”雨)。
请帮帮我,已经解决了一个星期。 :(
这是我的代码:
在我的主要活动的 onCreate() 方法中:
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
countries = getResources().getStringArray(R.array.countries_array);
textView = (AutoCompleteTextView) findViewById(R.id.autocomplete_country);
TextWatcher textChecker = new TextWatcher() {
@Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
// TODO Auto-generated method stub
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
String enteredText = textView.getText().toString();
refreshList(enteredText);
textView.showDropDown();
}
};
textView.addTextChangedListener(textChecker);
}
public void refreshList(String text) {
adapter = new ArrayAdapter<String>(this, R.layout.list_item, fetchTop20(countries, text));
textView.setThreshold(1);
textView.setAdapter(adapter);
}
public List<String> fetchTop20(String[] countries, String strToFind){
List<String> arrList_countries = Arrays.asList(countries);
List<String> arrList_strTop20 = new ArrayList<String>();
int ctrToTwenty = 0;
for(String currCountry: arrList_countries)
{
if(currCountry.toLowerCase().contains(strToFind.toLowerCase()))
{
arrList_strTop20.add(currCountry);
ctrToTwenty++;
}
if(ctrToTwenty == 20)
{
break;
}
}
Log.i(TAG, "strToFind: "+ strToFind);
Log.i(TAG, "strTop20: "+arrList_strTop20);
return arrList_strTop20;
}
我的 AutoCompleteTextBox 的适配器确实包含一个带有 [Bahamas, Bahrain] 的列表,但它的实际建议不包含任何内容。如果您输入“Bah”,则此方法有效(尽管您应该输入单词的第一个字母才能显示):(。
android 专家有什么建议吗?另外,由于我的代码的自动完成过滤没有在单独的线程中运行,请告诉我如何优化这个或告诉我如何线程化这个代码进行优化。谢谢。
【问题讨论】:
标签: android autocomplete filtering