本文介绍了Android的自动完成文本框,设定自己的具体建议?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些技术上的问题在这里,比如我有一个自动完成文本框包含此列表中,[巴林,白俄罗斯,巴哈马]适配器。如果我输入例如啊,我想在autocomplete_textbox这两个下拉列表显示,[巴林,巴哈马],因为这两个包含啊(B啊AMAS和B的子串啊雨)。

I have some technical problems here, for example I have an adapter for autocomplete textbox that contains this list, [ Bahrain, Belarus, Bahamas]. If I typed for example "ah", I want to display in the drop-down of autocomplete_textbox these two, [Bahrain, Bahamas], since those two contains a substring of "ah" (B"ah"amas and B"ah"rain).

请帮我,已经搞清楚了这一点了一个星期。 (

Please help me, been figuring this out for a week. :(

下面是我的code:

里面的onCreate()我的主要活动方式:

Inside onCreate() method of my main activity:

    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的适配器做包含[巴哈马,巴林]里面的列表,但其实际的建议包含什么。这工作如果键入呸(但应键入它来显示单词的第一个字母)。(

My AutoCompleteTextBox's adapter do contains a List with [Bahamas, Bahrain] inside, but its actual suggestion contains nothing. This works if you typed "Bah"(though you should type the first letter of the word for it to display):(.

任何建议Android的专家?还,因为我的code的自动完成过滤不是在一个单独的线程中运行,请告诉我如何优化这个或告诉我怎么线程这个code优化。谢谢。

Any suggestion android experts?, also, since my code's autocomplete filtering isn't running in a separate thread, please tell me how to optimize this or show me how to thread this code for optimization. thanks.

推荐答案

只需回答标题中的问题...

Just answering the question in the title...

-

阅读张贴的整个问题后...

After reading the whole question posted...

综观文档,它看起来像解决方案是让一个自定义的 ListAdapter 使用自定义的。

Looking at the docs, it looks like the solution is to make a custom ListAdapter with a custom Filter.

谷歌搜索打开了使用自定义过滤器的例子几秒钟:的

Google searching for a few seconds turned up an example of using a custom Filter: Autocomplete items disappearing

这篇关于Android的自动完成文本框,设定自己的具体建议?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 15:22