我有 2 个自定义适配器,用于 2 个具有默认起始值的微调器。我想根据 spinner1 的值将 spinner2 上的数组从起始 data2 更改为 data3 等。
public class SpinerKlasa extends Activity {
String[] data1 = { "V", "A", "K", "M", "T","R", "X", "C" };
String[] data2 = { " 750", "900", "1100","1300", "1500", "1700","1950", "2250", "2700"};
String[] data3 = { " 63", "73", "103","143", "163", "203","233", "253", "293"};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Spinner spinner1 = (Spinner) findViewById(R.id.spinner1);
spinner1.setAdapter(new MyAdapter(this, R.layout.spinner_rows, data1));
Spinner spinner2 = (Spinner) findViewById(R.id.spinner2);
spinner2.setAdapter(new MyAdapter2(this, R.layout.spinner_rows, data2));
spinner1.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int pos,
long arg3) {
if (pos == 1) {
//Here to change the array for spinner2 from data2 to data3
}
}
});
}
}
我的适配器在 SpinerKlasa 内。
这是科德:
public class MyAdapter extends ArrayAdapter<String> {
public MyAdapter(Context context, int textViewResourceId,
String[] objects) {
super(context, textViewResourceId, objects);
}
@Override
public View getDropDownView(int position, View convertView,
ViewGroup parent) {
return getCustomView(position, convertView, parent);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
return getCustomView(position, convertView, parent);
}
public View getCustomView(int position, View convertView,
ViewGroup parent) {
LayoutInflater inflater = getLayoutInflater();
View row = inflater.inflate(R.layout.spinner_rows, parent, false);
TextView label = (TextView) row.findViewById(R.id.textView1);
label.setText(data1[position]);
ImageView icon = (ImageView) row.findViewById(R.id.imageView1);
icon.setImageResource(images[position]);
return row;
}
}
//here I have the **MyAdapter2** class for spinner2 almost the same as MyAdapter class