简体   繁体   中英

Android autocompletetextview show previous input

我有一个AutoCompleteTextView ,但我希望它使用用户以前的输入,而不是我说要完成的某些字符串,所以问题是:我该怎么做?

If you want your AutoCompleteTextView to show suggestions based on some string rather than taking input from user then just call:

myAutoCompleteTextView.setText("cou", true);

and then in case drop down is not shown, call:

myAutoCompleteTextView.showDropDown();

AutoCompleteTextView do not propose this feature by default but you could implement it yourself.

  • storing each user input in a file or a database (I recommend to use a database)
  • retrieving those input and setting an adaptater to the AutoCompleteTextView .

Example to set an adpatater with pre defined string array from Google doc:

http://developer.android.com/reference/android/widget/AutoCompleteTextView.html

    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"
       };
    }

Instead to use a fix array COUNTRIES like in the example above, use a String array retrieved from the database,

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM