tags:

views:

334

answers:

2

Hi there,

its me again. I tried the last hours, how to change content of a spinner. ok, lets start from the beginning.

I have three spinners. They all have initial values. The first spinner is the main spinner and the other two spinners depend on the vale chosen in the first one. So i want to update the last two spinners after making a selection in spinner one. *edit: All spinners are on the same activity.

How can i achieve this? My problem is that i can only make changes in the spinners onitemselectadapter but thats a whole new class. I cannot reach the activity where my other spinners are.

thx

+2  A: 

Are your spinners in different activities?

If they are, so you can just pass the selected value of the first spinner via Intent (See the putExtra section) and retrieve the value from the next activity so that you can set accordingly the next spinners.

Edit:

Here is an example that changes the selected item in the 2nd and 3rd spinner. Update the listener (onItemSelected method) with your logic

Activity:

private Spinner s;
private Spinner s2;
private Spinner s3;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.main);
    String[] myList = new String[] { "Hello", "World", "Foo", "Bar" };
    String[] myList2 = new String[] { "Hello2", "World2", "Foo2", "Bar2" };
    String[] myList3 = new String[] { "Hello3", "World3", "Foo3", "Bar3" };

    s = (Spinner) findViewById(R.id.spinner1);
    s2 = (Spinner) findViewById(R.id.spinner2);
    s3 = (Spinner) findViewById(R.id.spinner3);

    s.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, myList));
    s2.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, myList2));
    s3.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, myList3));


    s.setOnItemSelectedListener(new OnItemSelectedListener(){

        @Override
        public void onItemSelected(AdapterView<?> parent, View v,
                int pos, long id) {
            s2.setSelection(pos);
            s3.setSelection(pos);
        }

        @Override
        public void onNothingSelected(AdapterView<?> arg0) {


        }});
}

main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:layout_width="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android"
         android:layout_height="wrap_content"
        android:orientation="vertical">
<Spinner android:id="@+id/spinner1" android:layout_height="wrap_content" android:layout_width="fill_parent" />
<Spinner android:id="@+id/spinner2" android:layout_height="wrap_content" android:layout_width="fill_parent" />
<Spinner android:id="@+id/spinner3" android:layout_height="wrap_content" android:layout_width="fill_parent" />
</LinearLayout>
ccheneson
hi ccheneson, no they are all one the same activity.
Andy
so what do you mean by "My problem is that i can only make changes in the spinners onitemselectadapter but thats a whole new class. I cannot reach the activity where my other spinners are."?
ccheneson
sry buddy, i was to stupid. You example works like a charm. Thank you.
Andy
A: 

I dont think I have understood this correctly as I am having a similar problem.

I have 2 spinners and both the spinners are in the sam activity.

Spinner 1 has Items {"Numerical","Roman"} which the user can select.

Based on the selection from spinner 1, spinner 2 will have the apporpriate items

So, if the user selects "Numerical" in spinner 1, Spinner 2 should show {"1","2"} and if the user selects "Roman" in spinner, Spinner 2 should show {"I", "II"}....

can someone clarify this for me please?

Mehool