1

I have to develop the application based on a spinner.

I have taken the spinner1 as the product choice and I have to give the spinner2 the data, which is in that particular product category so I have to update spinner2 on the base choice of spinner1.

I have search a lot but I can get any sure example code or resource for it.

iknow
  • 8,358
  • 12
  • 41
  • 68
Hrushikesh Betai
  • 2,227
  • 5
  • 33
  • 63
  • 2
    There're alot of similar questions here: http://stackoverflow.com/questions/4492003/dynamic-update-of-spinner2-based-on-choice-in-spinner-1 http://stackoverflow.com/questions/5453580/android-dynamic-spinner-update – ernazm Apr 08 '11 at 08:58

1 Answers1

1

I agree with @user639183, there are lot's of similar questions... however, there some explanation on how you do it:

  1. Create member variables for the arrays containing your options to display.
  2. Populate your first spinner with your category values.
  3. Hook on to the Spinner's OnItemSelected event by using spinner1.setOnItemSelectedListener(...)
  4. In the event listener, populate your second Spinner with the corresponding values.

Example for step 1:

private String[] spinner1values = new String[] { "cat1", "cat2" };
private String[][] spinner2values = new String[][] {
        new String[] { "a1", "b1", "c1" },
        new String[] { "a2", "b2" }
};

Population of spinner1 as follows:

Spinner spinner1 = (Spinner) findViewById(R.id.spinner1);
    ArrayAdapter<String> adapter1 = new ArrayAdapter<String>(this,
            android.R.layout.simple_spinner_item, spinner1values);
    adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinner1.setAdapter(adapter1);

This has so far been absolutely straight forward, if you read the documentation and examples for Spinners!

Next, hook on to the OnItemSelectedListener:

spinner1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> arg0, View arg1,
                int position, long arg3) {
            // create a new adapter with the corresponding values
            ArrayAdapter<String> adapter2 = new ArrayAdapter<String>(arg0.getContext(),
                    android.R.layout.simple_spinner_item, spinner2values[position]);
            adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
            // set adapter
            ((Spinner)findViewById(R.id.spinner2)).setAdapter(adapter2);
        }

        @Override
        public void onNothingSelected(AdapterView<?> arg0) {
            // nothing selected, so set empty options
            ArrayAdapter<String> adapter2 = new ArrayAdapter<String>(arg0.getContext(),
                    android.R.layout.simple_spinner_item, new String[0]);
            adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
            ((Spinner)findViewById(R.id.spinner2)).setAdapter(adapter2);
        }
    });

Pay attention, that the order of the arrays in spinner2values is corresponding to the order of the category values!

Michael Rose
  • 7,770
  • 3
  • 22
  • 26