我很新的
android.我想在我的应用程序中使用2个旋转器,一个显示国家列表,当选择任何国家时,其他微调器应该显示该国家/地区的城市列表.当选择城市时,执行一些动作. plz帮我一些示例代码.感谢预期
解决方法
这里是我们可以用来添加spinner2 w.r.t到spinner 1的选项的东西.
public class Activity extends Activity implements View.OnClickListener { private Spinner spinner0,spinner1,spinner2,spinner3; private Button submit,cancel; private String country[],state[],city[],area[]; Australia aus = new Australia(); Object object; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); spinner0 = (Spinner)findViewById(R.id.spinnerCountry); spinner1 = (Spinner)findViewById(R.id.spinnerQ1); spinner2 = (Spinner)findViewById(R.id.spinnerQ2); spinner3 = (Spinner)findViewById(R.id.spinnerQ3); submit = (Button)findViewById(R.id.btnSubmit); cancel = (Button)findViewById(R.id.btnCancel); submit.setOnClickListener(this); cancel.setOnClickListener(this); country = new String[] {"Select Country","Australia","USA","UK","New Zealand","EU","Europe","China","Hong Kong","India","Malaysia","Canada","International","Asia","Africa"}; ArrayAdapter<String> adapter0 = new ArrayAdapter<String>(Activity.this,android.R.layout.simple_spinner_item,country); adapter0.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line); spinner0.setAdapter(adapter0); Log.i("AAA","spinner0"); spinner0.setOnItemSelectedListener(new OnItemSelectedListener() { @Override public void onItemSelected(AdapterView<?> arg0,View view1,int pos,long id) { Log.i("AAA","OnItemSelected"); int loc; loc = pos; switch (loc) { case 1: state = aus.getState(); object = aus; Log.i("AAA","ArrayAdapter1"); ArrayAdapter<String> adapter1 = new ArrayAdapter<String>(Activity.this,state); adapter1.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line); spinner1.setAdapter(adapter1); Log.i("AAA","spinner1"); break; default: Log.i("AAA","default 0"); break; } } @Override public void onNothingSelected(AdapterView<?> arg1) { Log.i("AAA","Nothing S0"); } }); spinner1.setOnItemSelectedListener(new OnItemSelectedListener() { @Override public void onItemSelected(AdapterView<?> arg0,long id) { Log.i("AAA","OnItemSelected S1"); int loc = pos; switch(loc) { case 1: Log.i("AAA","Australia"); if(object.equals(aus)) { city = aus.getType(loc); } else { break; } ArrayAdapter<String> adapter = new ArrayAdapter<String>(Activity.this,city); adapter.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line); spinner2.setAdapter(adapter); Log.i("AAA","spinner2"); break; default: Log.i("AAA","default 1"); break; } } @Override public void onNothingSelected(AdapterView<?> arg0) { Log.i("AAA","Nothing S1"); } }); spinner2.setOnItemSelectedListener(new OnItemSelectedListener() { @Override public void onItemSelected(AdapterView<?> arg0,View arg1,long id) { int loc = pos; switch (loc) { case 1: if(object.equals(aus)) { area = aus.getTitle(loc); } ArrayAdapter<String> adapter = new ArrayAdapter<String>(Activity.this,area); adapter.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line); spinner3.setAdapter(adapter); Log.i("","spinner3"); break; default: break; } } @Override public void onNothingSelected(AdapterView<?> arg0) { // TODO Auto-generated method stub } }); }// on-create @Override public void onClick(View v) { switch (v.getId()) { case R.id.btnSubmit: break; case R.id.btnCancel: finish(); break; default: break; } } }
如果你觉得这很有用,那就放弃投票,让别人可以很容易地找到一个很好的答案.
对于每个国家,你必须为它创建一个课程,只需添加州,城市&区.这样一来,它就不会变成一个单一的网格.
玩的开心.
问候,
HAPS.