我想根据在第一个微调器中选择的值从第二个微调器中选择值。例如,我的第一个微调器显示国家名称,第二个微调器显示相应国家的城市名称。问题是第二个微调器始终是第一个选择的值。示例 - 第一个微调器是国家名称 - 印度,巴基斯坦,中国,第二个微调器是城市名称,所以如果我选择印度和我的微调器是-dhhi,up,旁遮普,那么它总是得到选择值作为德里总是。当我改变了所选的值,即使onItemSelected函数没有再次调用。相同的代码是 -
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);
}
}
}
}
答案 0 :(得分:1)
解决了我的问题。我只需要为第二个微调器实现onItemSelectListener。现在更新的代码是 -
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
}
}
答案 1 :(得分:0)
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
}
});
}
}