开发者

Android AutoComplete TextBox, setting your own specific suggestions?

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. :(

Here's my code:

Inside onCreate() method of my main activity:

    super.onCreate(savedInstanceState);
    setCont开发者_运维知识库entView(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;
}  

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):(.

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...

Custom AutoComplete in Android

http://developer.android.com/resources/tutorials/views/hello-autocomplete.html

--

After reading the whole question posted...

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜