Skip to content Skip to sidebar Skip to footer

How To Set Spinner Selection By Text Inside It

I preparing a form in which i have to use same page for Adding details and Editing details. While adding details all fields will be blank and spinner selection will be set to 'no s

Solution 1:

This is what i did and it seems to work fine

Spinner my_spinner=(Spinner)findViewById(R.id.spn_items);
ArrayAdapter<String> array_spinner=(ArrayAdapter<String>)my_spinner.getAdapter();
    my_spinner.setSelection(array_spinner.getPosition("list item"));

Solution 2:

I dont now how frequently this might be used but we can set selection of the spinner by text inside it. Spinner has the method setSelection(int position);.

Now in the parameter we need to pass position of the text, which we can get from the array_list we use to bind to adapter, by getIndexOf(Object object) and object should be of the type of ArrayList that is declared For example, if ArrayList is of type String, the object to be passed to getIndexOf(Object object) should be of type String.

Finally, you set selection as below:

spinner.setSelection ( spinner_array_list.indexOf(string) );

Post a Comment for "How To Set Spinner Selection By Text Inside It"