Android: AutoCompleteTextView with default suggestions

倖福魔咒の 提交于 2020-01-11 04:55:33

问题


How do I show some default suggestions for AutoCompleteTextView before the user type anything? I cannot find a way to do this even with creating a custom class that extends AutoCompleteTextView.

I want to show suggestions for common input values to save the user from typing.

Any suggestions?


回答1:


You should subclass AutoCompleteTextView and override enoughToFilter() to return true all the time. After that you can call performFiltering("",0) (it's a protected function, so you can export this call via a public function in your class).

Something like that:

public class ContactsAutoCompleteTextView extends AutoCompleteTextView {


    public ContactsAutoCompleteTextView(Context context) {
        super(context);
    }

    public ContactsAutoCompleteTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public ContactsAutoCompleteTextView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    @Override
    public boolean enoughToFilter() {
        return true;
    }

    public void temp() {
        performFiltering("",0);
    }
}



回答2:


Itay Kahana's answer is indeed correct. The only thing I would add is that instead of creating a temp() function, to override the onFocusChanged function. Personally I used the following:

 @Override
        protected void onFocusChanged (boolean focused, int direction, Rect previouslyFocusedRect) {
            if(focused)
                performFiltering("", 0);
            super.onFocusChanged(focused, direction, previouslyFocusedRect);
        }



回答3:


If you dont need it to be dynamic I would go by having a string array in the resources, and then just load the array when the AutoCompleteTextView is about to be viewed. Like:

 public class CountriesActivity extends Activity {
     protected void onCreate(Bundle icicle) {
         super.onCreate(icicle);
         setContentView(R.layout.countries);

         ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
                 android.R.layout.simple_dropdown_item_1line, COUNTRIES);
         AutoCompleteTextView textView = (AutoCompleteTextView)
                 findViewById(R.id.countries_list);
         textView.setAdapter(adapter);
     }

     private static final String[] COUNTRIES = new String[] {
         "Belgium", "France", "Italy", "Germany", "Spain"
     };
 }

Which can be found on http://developer.android.com/reference/android/widget/AutoCompleteTextView.html

Another way which I have done a couple of times which allows it to learn from the user is o use a database connection with IE a simple cursor. When you create the db you could insert some default values. Here´s an example with using simple cursor adapter: http://androidcommunity.com/forums/f4/how-to-use-autocompletetextview-with-simplecursoradapter-15875/

Edit 1:

One idea to show the list before the user starts type is to have a simple listview below the EditText. Not sure if you could call the autocompletetextview to show the suggestions, should be possible somehow. Perhaps you need to create your own autocompletetextiew class.



来源:https://stackoverflow.com/questions/6618409/android-autocompletetextview-with-default-suggestions

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!