2
votes

http://developer.android.com/resources/tutorials/views/hello-spinner.html I use this link as the base..

I have 3 spinners, first spinner for country , second for states and third for cities.

I had already seen the links on the stackoverflow , but it is not that much proper.

my code is as follows

public class SpinnerActivity extends Activity {

String state="";

ArrayAdapter adapter1,adapter2; Spinner spinner1,spinner2;

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

    spinner1 = (Spinner) findViewById(R.id.spinner1);
    spinner2 = (Spinner) findViewById(R.id.spinner2);

   adapter1 = ArrayAdapter.createFromResource(
           this, R.array.states_array, android.R.layout.simple_spinner_item);
   adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
   spinner1.setAdapter(adapter1);
   spinner1.setOnItemSelectedListener(new MyOnItemSelectedListener());


   if(state.equalsIgnoreCase("Gujarat"))
   {  
    adapter2 = ArrayAdapter.createFromResource(this, R.array.rajasthan_array, >android.R.layout.simple_spinner_item);
    }
  if(state == "Rajasthan")
   {
    adapter2 = ArrayAdapter.createFromResource(this, R.array.rajasthan_array, >android.R.layout.simple_spinner_item);
   }

   adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
   spinner2.setAdapter(adapter2);
   spinner2.setOnItemSelectedListener(new MyOnItemSelectedListener());

}

public class MyOnItemSelectedListener implements OnItemSelectedListener {

   public void onItemSelected(AdapterView<?> parent,View view, int pos, long id)
   {
      state=parent.getItemAtPosition(pos).toString();
     Toast.makeText(parent.getContext(), "The state is " +
         parent.getItemAtPosition(pos).toString(), Toast.LENGTH_SHORT).show();
   }
   public void onNothingSelected(AdapterView parent)
   {
    //    Do nothing.
   }

}

}

the bug I found is , I have created all he thing in the onCreate() method.. but I do not find the alternate thing that can I do ... I don't know why this not work ..

please help if possible

thanks...

1
if one spinner enter the Country India , then the second spinner shows the states of India ,.... , but this code do not work somehowMunjal Upadhyay

1 Answers

0
votes

In onSelectItem fill second spinner and so on.