我有2个自定义适配器,用于2个具有默认起始值的微调器。 我想将spinner2上的数组从启动data2更改为data3等。取决于spinner1的值。
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内 这是kode:
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