vijay

welcome Netizen

Share Your Knowledge.It is a way to achieve immortality

Friday, August 31, 2012

How to set the selected item by data or position on spinner in android


When using spinner in android we set the data to spinner using adapter.

Normally we get the data in array string and we store the value in spinner using adapter.
In some situation we need select the data through dynamic at the time we need to follow the below process to set the data in Spinner.
But spinner has to allow set the data only by positions. But we need to choose by data like dropdown list.
In that situation we have to follow the below process….

String[] name;
name =new String[]{"name1","name2","name3","name4","name5"};
ArrayAdapter adapter = new ArrayAdapter(this,
                                                                                android.R.layout.simple_spinner_item, name);
spinccy.setAdapter(adapter);

IF we want to set spinner selected data as “name3”
First we find the position of the data in adapter
int Position = adapter.getPosition(“name3”);
spinccy.setSelection(Position);

Thast’it this process you need to follow to set the data by value in spinner.
Happy Coding………….

0 comments:

Post a Comment