Take the 2-minute tour ×

I am developing an app where i need to set spinner values dynamically based on previous screen values. My code...


String[] values = {"All","Only Walk-in","Only Phone","Only Web","Walkin-phone","Walkin-web","phone-web"};

 /*ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item,apttypes);

But here what i want is from previous screen i am getting some value (like spinner postion). based on that i need to set spinner value to display...

Means from previous screen if i got values =0 means,

i need to set spinner value to display "All" from values array at top.

If i got value= 5 means,

i want to set spinner value to display as "Walkin-web"

How can i do that. can anyone help me with this...

share|improve this question
Did you get the Solution?? –  Arivazhagan Nov 9 '12 at 12:17
Did you try in anyother methods? Share yours source code.. –  gowri Nov 9 '12 at 12:19

5 Answers 5

up vote 1 down vote accepted

Pass the value in from the previous Activity using extras in the Intent you use to launch it. Then when you've read the value call

int position = getIntent().getIntExtra( "name", defaultValue );
spinner.setSelection( position );

Which will move the spinner to the index you selected.

share|improve this answer
can i set the spinner value by its value not by setting the position?? –  Subhalaxmi Nayak Jan 11 at 12:05
Not directly but you can always read through your values to find the index of the one you want and set that. –  ScouseChris Jan 14 at 9:17

Use following array code and create new array adapter each time

 string[] str=new string[maxsize-4];
share|improve this answer
you can implement onItemClick event on Spinner like this and setSelection(position)

        //Spinner OnItemClick Event here
        payfeeTabStudentNameSpinner.setOnItemSelectedListener(new OnItemSelectedListener(){

                    int position, long id) {
                spinnerSelectedValue = parent.getItemAtPosition(position).toString();
            public void onNothingSelected(AdapterView<?> arg0) {
                // TODO Auto-generated method stub
share|improve this answer

Inside your First Activity A.java

public static String Position = YOUR_SPINNER1.getSelectedItem().toString();

Inside your Second Activity B.java

//Set your adapter accordingly
 }else if(A.Position.equals("1")){
//Set your adapter accordingly
share|improve this answer

You can assign the Spinner's Position using the following code..

 Spinner s1;
 int position=valueFromPrevious;
share|improve this answer
You can assign the Value of the Spinner from the Previous Class by the Above assignment Statement.. –  gowri Nov 9 '12 at 12:04
Yeah it will works Good... –  Arivazhagan Nov 9 '12 at 12:05
final String valueFromPrevious = getIntent().getExtras().getString("SpinnerPosition"); You can use this for the Value get from Previous.. –  gowri Nov 9 '12 at 12:14
It will sounds in the Solution.. –  MGR Nov 9 '12 at 12:16

Your Answer

