当我选择中国作为第一个旋转器的国家时,看到我想要的,所以我希望第二个旋转器必须显示中国的所有状态它是通过我的编码来完成的......但是......! 我的查询是这样的,当我从第二个微调器中选择状态时,它会自动将其设置为第二个微调器的第一个值 就像我在第二个微调器中有3个值 -“上海” -“北京” -“南京” 当我选择北京时,它会自动返回上海.. !! 帮帮我..!! :)
public class MainActivity extends Activity implements AdapterView.OnItemSelectedListener
{
String[] country = { "India", "Pakistan", "China"};
String[] states_india = { "Delhi", "UP", "Bihar"};
String[] states_china = { "Shanghai", "Beijing", "Nanjing"};
String[] states_pak = { "Lahor", "Islamabaad", "Punjab"};
int fee,tot=0,atot=0;
Spinner spin0,spin1;
private RadioGroup radioSexGroup;
RadioButton r1,button1,button2;
private EditText edittext1,edittext2;
String Name;
private TextView calc,detail;
private Button clean;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
spin0 = (Spinner) findViewById(R.id.spinner1);
spin0.setOnItemSelectedListener((OnItemSelectedListener) this);
ArrayAdapter <String> c = new ArrayAdapter <String> (this,android.R.layout.simple_spinner_item,country);
c.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spin0.setAdapter(c);
spin1 = (Spinner) findViewById(R.id.spinner2);
spin1.setOnItemSelectedListener((OnItemSelectedListener) this);
spin1.setEnabled(false);
//<!-- © 2013 Parth Shrarma -->
addListenerOnButton();
}
private void addListenerOnButton() {
radioSexGroup = (RadioGroup) findViewById(R.id.radioGroup1);
button1 = (RadioButton) findViewById(R.id.radioButton1);
button2 = (RadioButton) findViewById(R.id.radioButton2);
edittext1=(EditText) findViewById(R.id.editText1);
edittext2=(EditText) findViewById(R.id.editText2);
detail=(TextView) findViewById(R.id.textView5);
calc=(TextView) findViewById(R.id.textView6);
calc.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
StringBuilder result=new StringBuilder();
if(!edittext1.getText().equals("") && edittext1.getText().length() > 0 )
{
Name = edittext1.getText().toString();
}
fee = Integer.parseInt(edittext2.getText().toString());
int selectedId = radioSexGroup.getCheckedRadioButtonId();
r1 = (RadioButton) findViewById(selectedId);
result.append("Details : \nName"+ Name);
}});
clean=(Button) findViewById(R.id.clear);
clean.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
edittext1.setText("");
detail.setText("Total : ");
}
//<!-- © 2013 Parth Shrarma -->
});
}
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,long arg3) {
spin1.setEnabled(true);
if(spin0.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);
spin1.setAdapter(s1);
}
else if(spin0.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);
spin1.setAdapter(s2);
}
else if(spin0.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);
spin1.setAdapter(s3);
}
}
public void onNothingSelected(AdapterView<?> arg0) {
}
}
答案 0 :(得分:7)
问题可能在于 ItemSelectedListener 。在为两个微调器设置相同的侦听器时,必须检查选择了哪个微调器。您可以使用AdapterView<?> arg0
参数进行检查。
在这里,您反复拨打setAdapter()
。
所以,你的代码会改变,
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,long arg3) {
if(arg0.equals(spin0)){
spin1.setEnabled(true);
if(spin0.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);
spin1.setAdapter(s1);
}
else if(spin0.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);
spin1.setAdapter(s2);
}
else if(spin0.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);
spin1.setAdapter(s3);
}
}
}