我正在尝试更改两个Spinners的内容。所以我创造了我的Spinners:
Spinner spinner01 = (Spinner) findViewById(R.id.Spinner01);
Spinner spinner02 = (Spinner) findViewById(R.id.Spinner02);
ArrayAdapter<CharSequence> adapter1 = ArrayAdapter.createFromResource(
this, R.array.MetricUnitsArray, android.R.layout.simple_spinner_item);
ArrayAdapter<CharSequence> adapter2 = ArrayAdapter.createFromResource(
this, R.array.MetricUnitsArray, android.R.layout.simple_spinner_item);
adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner01.setAdapter(adapter1);
spinner02.setAdapter(adapter2);
spinner01.setOnItemSelectedListener(new MyOnItemSelectedListener1());
spinner02.setOnItemSelectedListener(new MyOnItemSelectedListener2());
现在从不同的方法,我希望能够做到这样的事情:
adapter1 = ArrayAdapter.createFromResource(
this, R.array.ImperialUnitsArray, android.R.layout.simple_spinner_item);
adapter2 = ArrayAdapter.createFromResource(
this, R.array.ImperialUnitsArray, android.R.layout.simple_spinner_item);
无论如何都要让适配器从不同的String数组中读取?或者我应该用新的值替换字符串数组中的值吗?
答案 0 :(得分:1)
为要分配给spinner2的每个数组创建单独的适配器。 声明他们是最终的。 在设置spinner1 ItemSelected Listener之前放置适配器声明。 在spinner1 ItemSelected侦听器中,根据需要设置适当的适配器。
在下面的代码中,我有两个微调器。 spinner1有4个包含传感器类型的条目,当选择其中一个传感器类型时,spinner2将填充该传感器类型的各种模型。
public class MainActivity extends Activity {
void showToast(CharSequence msg) {
Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final Spinner s1 = (Spinner) findViewById(R.id.spinner_sensor_type);
final Spinner s2 = (Spinner) findViewById(R.id.spinner_sensor_model);
final ArrayAdapter<CharSequence> sensor_adapter = ArrayAdapter.createFromResource(
this, R.array.sensors,android.R.layout.simple_spinner_item);
sensor_adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
final ArrayAdapter<CharSequence> pa_adapter = ArrayAdapter.createFromResource(
this, R.array.pa_models,android.R.layout.simple_spinner_item);
final ArrayAdapter<CharSequence> ca_adapter = ArrayAdapter.createFromResource(
this, R.array.ca_models,android.R.layout.simple_spinner_item);
final ArrayAdapter<CharSequence> ni_adapter = ArrayAdapter.createFromResource(
this, R.array.ni_models,android.R.layout.simple_spinner_item);
final ArrayAdapter<CharSequence> ni_fe_adapter = ArrayAdapter.createFromResource(
this, R.array.ni_fe_models,android.R.layout.simple_spinner_item);
pa_adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
ca_adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
ni_adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
ni_fe_adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
s1.setAdapter(sensor_adapter);
s2.setAdapter(pa_adapter); // set default array values in spinner 2
s1.setOnItemSelectedListener(
new OnItemSelectedListener() {
public void onItemSelected(
AdapterView<?> parent, View view, int position, long id) {
showToast("Sensor Type: position=" + position + " id=" + id);
switch (position) {
case 0:
s2.setAdapter(pa_adapter);
break;
case 1:
s2.setAdapter(ca_adapter);
break;
case 2:
s2.setAdapter(ni_adapter);
break;
case 3:
s2.setAdapter(ni_fe_adapter);
default: // Do this...
break;
}
}
public void onNothingSelected(AdapterView<?> parent) {
showToast("Sensor Type: unselected");
}
});
s2.setOnItemSelectedListener(
new OnItemSelectedListener() {
public void onItemSelected(
AdapterView<?> parent, View view, int position, long id) {
showToast("Sensor Model: position=" + position + " id=" + id);
}
public void onNothingSelected(AdapterView<?> parent) {
showToast("Sensor Model: unselected");
}
});
}
}
答案 1 :(得分:0)
您可以使用ArrayAdapter
的getter / setter:
E.g:
adapter1.clear();
adapter1.addAll(theNewListOfValues);
adapter1.notifyDatasetChanged();
答案 2 :(得分:0)
您应该可以使用新适配器在您的微调器上调用setAdapter。我建议您在创建活动后将适配器存储为活动中的字段,以便在需要再次在适配器之间切换时可以轻松地重新使用它们。