我有两个旋转器。乡村和城市。我想在选择乡村时动态地改变城市的价值观。我知道如何创建和初始化乡村的价值,但不知道如何设置和改变城市的价值观。
如有任何指导,将不胜感激。
UPDATES1问题是,我不知道如何更新城市旋转器的内容。我知道如何创建固定的旋转器的侦听器和其他基础知识。
发布于 2011-09-27 00:32:48
对于第二个旋转器,在List<String>上使用Adapter (或者不管您的城市表示是什么)。更改列表的内容后,调用适配器上的notifyDataSetChanged,这样就可以了。
发布于 2011-09-27 00:38:48
您需要获得对旋转器的编程引用,如下所示:
Spinner spinner = (Spinner) findViewById(R.id.spinner);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
this, R.array.planets_array, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);然后,要更新城市的值,请使用OnItemSelectedListener,如下所示:
public class MyOnItemSelectedListener implements OnItemSelectedListener {
public void onItemSelected(AdapterView<?> parent,
View view, int pos, long id) {
//update content of your city spinner using the value at index,
// id, or the view of the selected country.
}
}
public void onNothingSelected(AdapterView parent) {
// Do nothing.
}}
最后,您需要像这样将侦听器绑定到国家旋转器:
spinner.setOnItemSelectedListener(new MyOnItemSelectedListener());参见这里的参考:http://developer.android.com/resources/tutorials/views/hello-spinner.html
https://stackoverflow.com/questions/7562922
复制相似问题