最初のスピナーで国として中国を選択したときに欲しいのを見てください。したがって、2番目のスピナーで中国のすべての州を表示する必要があります。これは、このコーディングによって行われます.. 私のクエリは、2 番目のスピナーから状態を選択すると、自動的に 2 番目のスピナーの 1 番目の値として設定されます。たとえば、2 番目のスピナーに「上海」-「北京」-「南京」という 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) {
}
}