I want to select value from second spinner on the basis of value selected in first spinner.For example my first spinner displays country name and second spinner displays city name of that respective country.The problem is that second spinner is always taking first value as selected.Example-first spinner is country name-India,pakistan,china and second spinner is cities name,so if i select india and my spinner is-delhi,up,punjab then it always get selected value as delhi always.When I change the selected value even the function onItemSelected is not calling again.Code for the same is-
public class LaunchCampaign1 extends Activity implements OnItemSelectedListener,OnClickListener
{
String[] countryStr = {"Select", "India", "Pakistan", "China"};
String[] states_india = {"Select", "Delhi", "UP", "Bihar"};
String[] states_china = { "Select","Shanghai", "Beijing", "Nanjing"};
String[] states_pak = { "Select","Lahor", "Islamabaad", "Punjab"};
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_launch_campaign);
country=(Spinner)findViewById(R.id.spinnerCountry);
country.setOnItemSelectedListener((OnItemSelectedListener) this);
ArrayAdapter <String> c = new ArrayAdapter <String> (this,android.R.layout.simple_spinner_item,countryStr);
c.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
country.setAdapter(c);
city=(Spinner)findViewById(R.id.spinnerCity);
city.setOnItemSelectedListener((OnItemSelectedListener) this);
city.setEnabled(false);
public void onItemSelected(AdapterView<?> arg0, View view, int position,long id)
{
if(arg0.equals(country))
{
city.setEnabled(true);
if(country.getSelectedItem().equals("India"))
{
ArrayAdapter <String> s1 = new ArrayAdapter <String> (this,android.R.layout.simple_spinner_item,states_india);
s1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
city.setAdapter(s1);
/*city.setSelection(position);
Log.e("pos",""+position);
String cityStr = (String) city.getSelectedItem();*/
String cityStr=(String) city.getSelectedItem();
Log.e("city",cityStr);
}
else if(country.getSelectedItem().equals("Pakistan"))
{
ArrayAdapter <String> s2 = new ArrayAdapter <String> (this,android.R.layout.simple_spinner_item,states_pak);
s2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
city.setAdapter(s2);
}
else if(country.getSelectedItem().equals("China"))
{
ArrayAdapter <String> s3 = new ArrayAdapter <String> (this,android.R.layout.simple_spinner_item,states_china);
s3.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
city.setAdapter(s3);
}
}
}
}
Got the solution of my problem.I just need to implement the onItemSelectListener for second spinner also.Now the updated code is-
public class LaunchCampaign1 extends Activity implements OnClickListener,OnItemSelectedListener
{
String[] countryStr = {"Select", "India", "Pakistan", "China"};
String[] states_india = {"Select", "Delhi", "UP", "Bihar"};
String[] states_china = { "Select","Shanghai", "Beijing", "Nanjing"};
String[] states_pak = { "Select","Lahor", "Islamabaad", "Punjab"};
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_launch_campaign);
country=(Spinner)findViewById(R.id.spinnerCountry);
country.setOnItemSelectedListener((OnItemSelectedListener) this);
ArrayAdapter <String> c = new ArrayAdapter <String> (this,android.R.layout.simple_spinner_item,countryStr);
c.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
country.setAdapter(c);
city=(Spinner)findViewById(R.id.spinnerCity);
city.setOnItemSelectedListener((OnItemSelectedListener) this);
city.setEnabled(false);
public void onItemSelected(AdapterView<?> parent, View view, int position,long id)
{
switch(parent.getId())
{
case R.id.spinnerCountry:
city.setEnabled(true);
if(country.getSelectedItem().equals("India"))
{
ArrayAdapter <String> s1 = new ArrayAdapter <String> (this,android.R.layout.simple_spinner_item,states_india);
s1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
city.setAdapter(s1);
}
else if(country.getSelectedItem().equals("Pakistan"))
{
ArrayAdapter <String> s2 = new ArrayAdapter <String> (this,android.R.layout.simple_spinner_item,states_pak);
s2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
city.setAdapter(s2);
}
else if(country.getSelectedItem().equals("China"))
{
ArrayAdapter <String> s3 = new ArrayAdapter <String> (this,android.R.layout.simple_spinner_item,states_china);
s3.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
city.setAdapter(s3);
}
break;
case R.id.spinnerCity:
String cityStr1=city.getSelectedItem().toString();
Log.e("city1",cityStr1);
}
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
// TODO Auto-generated method stub
}
}
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
public class Data1Activity extends Activity {
String[] countryStr = { "India", "China","Pakistan"};
String[] states_india = { "Delhi", "UP", "Bihar"};
String[] states_china = { "Shanghai", "Beijing", "Nanjing"};
String[] states_pak = { "Lahor", "Islamabaad", "Punjab"};
Spinner country,city;
ArrayAdapter<String> adaptercountry;
ArrayAdapter<String> adaptercity;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main1);
country= (Spinner) findViewById(R.id.spinner1);
city = (Spinner) findViewById(R.id.spinner2);
adaptercountry = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_1,countryStr);
country.setAdapter(adaptercountry);
country.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
if(position==0)
{
adaptercity = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_1,states_india);
}
else
if(position==1)
{
adaptercity = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_1,states_china);
}
else
if(position==2)
{
adaptercity = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_1,states_pak);
}
city.setAdapter(adaptercity);
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
// TODO Auto-generated method stub
}
});
}
}