user1578518
user1578518

Reputation:

Setting spinner position dynamically in android?

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

Main.java.

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);
    spinner.setAdapter(adapter);*/

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...

Upvotes: 1

Views: 12034

Answers (6)

Shyam Deore
Shyam Deore

Reputation: 127

Use following array code and create new array adapter each time

 String[] str=new String[maxsize-4];

Upvotes: 3

sagar potdar
sagar potdar

Reputation: 608

pass the values from previous activity using Extras and then when u want use it in your current activity follow this:

If u get String value then typecast it into interger by parseInt method...

String strtext4 = getIntent().getStringExtra("qualification");

then

int position = Integer.parseInt(strtext4);

after this just set it to your spinner

qualificationspinner.setSelection(position);

Upvotes: 0

gowri
gowri

Reputation: 681

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

 Spinner s1;
 -----------
 -----------
 int position=valueFromPrevious;
 s1.setSelection(position);
 -----------
 -----------

Upvotes: 0

Bhavesh Patadiya
Bhavesh Patadiya

Reputation: 25830

Inside your First Activity A.java

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

Inside your Second Activity B.java

    if(A.Position.equals("0")){
//Set your adapter accordingly
 }else if(A.Position.equals("1")){
//Set your adapter accordingly
 }

Upvotes: 0

Veerababu Medisetti
Veerababu Medisetti

Reputation: 2755

you can implement onItemClick event on Spinner like this and setSelection(position)

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

            @Override
            public void onItemSelected(AdapterView<?> parent, View view,
                    int position, long id) {
                payfeeTabStudentNameSpinner.setSelection(position);
                spinnerSelectedValue = parent.getItemAtPosition(position).toString();
            }
            @Override
            public void onNothingSelected(AdapterView<?> arg0) {
                // TODO Auto-generated method stub
            }
        });

Upvotes: 1

ScouseChris
ScouseChris

Reputation: 4397

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.

Upvotes: 9

Related Questions