tags:

views:

77

answers:

2

I want to have dynamic spinner in android e.g one spinner for country and depending upon the value selected in Country, i want to get another spinner for states.

+2  A: 

I believe you can solve this with the OnItemSelectedListener.

public void onCreate() {
     ....
    Country[] mCountries = ... ;
    final Spinner spinner1 = ...;
    final Spinner spinner2 = ...;

    spinner1.setAdapter(new ArrayAdapter(mCountries);
    spinner1.setOnItemSelectedListener( new OnItemSelectedListener() {

    void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
        Country country = (Country) parent.getAdapter().getItem(position);
        spinner2.setAdapter(new ArrayAdapter(country.getStates());
     }
     void onNothingSelected(AdapterView<?> parent) {
        spinner2.setAdapter(null);
     }
    });
 ....
}
Greg
Thanks Greg for your help. But what if the number of spinner keeps growing dynamically. e.g if we select states then we should have another spinner for district and so on.
Ranjeet
Hi Ranjeet, you can probably just use a list of spinners and apply the same algorithm in a loop. The list should be ordered by most general spinner to most specific. Or you could even create the new spinner and add it to the view hierarchy within your on item selected listener.
Greg
Thanks again Greg. This is what I was looking for. One more help please. Can we have more than 1 value associated with Spinner. e.g. can we have Spinner having country and its countrycode as its id. This is just like our normal html way.
Ranjeet
When you are creating the spinner you can probably call setTag on the newly created spinner and pass the appropriate country code as the value. Then you should be able to read that out via getTag
Greg
Greg, is there any way that by default nothing is selected in our Spinner?
Ranjeet
Oh course :) You could make something like, Country.NONE("Please select a valid Country"), or use null as an element of the array of countries. Then inside of the first spinner's onSelectedItemListner, if the value selected is null, then remove all other spinners. This pattern can be repeated for the last ones too. (This is starting to get rather involved).
Greg
A: 

I have the same problem, but I am getting an error: Cannot instantiate the type AdapterView.OnItemSelectedListener

This is driving me crazy can someone help!

package com.Landman;

import android.app.Activity; import android.content.DialogInterface; import android.os.Bundle; import android.view.View; import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.EditText; import android.widget.Spinner; import android.widget.AdapterView.OnItemClickListener; import android.widget.AdapterView.OnItemSelectedListener;

public class Landman extends Activity{ Spinner spinner, spinner2; EditText EditText01, EditText02;

/** Called when the activity is first created. 
 * @param l */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);


    spinner = (Spinner) findViewById(R.id.spinner);
    spinner2 = (Spinner) findViewById(R.id.Spinner2);
    EditText01 = (EditText) findViewById(R.id.EditText01);
    EditText02 = (EditText) findViewById(R.id.EditText02);


    ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
            this, R.array.conversion_array, android.R.layout.simple_spinner_item);
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinner.setAdapter(adapter);

    spinner.setOnItemSelectedListener(new OnItemSelectedListener());

    void onItemSelected(AdapterView<?> parent, View view, int position, long id){
        ArrayAdapter<CharSequence> Acre_adapter = ArrayAdapter.createFromResource(
                this, R.array.acre_array, android.R.layout.simple_spinner_item);
        Acre_adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

        spinner2.setAdapter(Acre_adapter);
    }

    void onNothingSelected(AdapterView<?> parent) {

    }

}

Alan
try this: spinner.setOnItemSelectedListener( new OnItemSelectedListener() { void onItemSelected(AdapterView<?> parent, View view, int position, long id) { ArrayAdapter<CharSequence> Acre_adapter = ArrayAdapter.createFromResource( this, R.array.acre_array, android.R.layout.simple_spinner_item); Acre_adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); spinner2.setAdapter(Acre_adapter); } void onNothingSelected(AdapterView<?> parent) { spinner2.setAdapter(null); } });
Ranjeet
Also you should not create ArrayAdapter using createFromResouce(). In stead you should do following: adapter= new ArrayAdapter<CharSequence>(this, android.R.layout.simple_spinner_item, listName); adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
Ranjeet