Jose Manuel
Jose Manuel

Reputation: 807

Setting value in AutoCompleteTextView

I have a AutoCompleteTextView with a list of items, and I need select one of them...

I am doing something like:

myAutoCompleteTextView.setListSelection( index);

and...

myAutoCompleteTextView.setText( index);

but don't work... How can I set a item by default?

Upvotes: 7

Views: 20413

Answers (3)

Samiya Khan
Samiya Khan

Reputation: 369

You can set the default value of AutoCompleteTextView using following ways:

  1. Using position or index

    myAutoCompleteTextView.setText(adapter.getItem(1),false);

  2. Using the value or string

    myAutoCompleteTextView.setText(value,false);

  3. If you want to do the filter also, then remove the false.

    myAutoCompleteTextView.setText(value);

Upvotes: 3

Saljith Kj
Saljith Kj

Reputation: 145

setText(CharSequence text, boolean filter) if you don't want filter. You can make it false:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
        myAutoCompleteTextView.setText(adapter.getItem(2),false);
      }

Upvotes: 8

Jared Burrows
Jared Burrows

Reputation: 55517

This will not work because setText takes a CharSequence.

myAutoCompleteTextView.setText(index);

public final void setText (CharSequence text)

Source: http://developer.android.com/reference/android/widget/TextView.html#setText(java.lang.CharSequence)

If you have a data structure such as a List<String> data, you can do something like this:

myAutoCompleteTextView.setText(data.get(index));

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

Upvotes: 15

Related Questions