AutoCompleteTextView autoCompleteTextView
ArrayAdapter<String> adapter;
ArrayList<String> list = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
// code...
list.add("First");
list.add("Second");
list.add("Third");
list.add("Forth");
list.add("Fifth");
autoCompleteTextView = findViewById(R.id.auto_complete);
adapter = new ArrayAdapter<>(this, R.layout.drop_down, list);
autoCompleteTextView.setAdapter(adpater);
}
then, after adding some strings to ArrayList and linking that list with autoCompleteTextView using Adapter.
autoCompleteTextView.setSelection(3);
Now I want any of my item getting selected automatically, once I open this activity. I treid setSelection() method, but it is not working.
CodePudding user response:
We need to pass false
as second argument to setText() method.
autoCompleteTextView.setText("First", false);
If we don't pass second arguiment "false"
as a filter to setText()
method, it will clear all the entries inside list and will keep just one entry. that we set as using setText()
method.
CodePudding user response:
your selected value will store selectedItem
@Override
protected void onCreate(Bundle savedInstanceState) {
// code...
autoCompleteTextView = findViewById(R.id.auto_complete);
list.add("First");
list.add("Second");
list.add("Third");
list.add("Forth");
list.add("Fifth");
adapter = new ArrayAdapter<>(this, R.layout.drop_down, list);
autoCompleteTextView.setAdapter(adpater);
autoCompleteTextView.setThreshold(1);
String selectedItem = autoCompleteTextView.getText().toString();
}