unitSpinner根据以下选择设置fromSpinner和toSpinner适配器:
unitSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
switch (position) {
case 0:
setUnitSpinnerAdapter(areaHeaders);
break;
.
.
.
case 12:
setUnitSpinnerAdapter(volumeHeaders);
break;
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
setUnitSpinnerAdapter:
public void setUnitSpinnerAdapter(String[] headers) {
ArrayAdapter<String> adapter = new ArrayAdapter<>(getActivity(),
android.R.layout.simple_spinner_item, headers);
adapterUnits.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
fromSpinner.setAdapter(adapter);
toSpinner.setAdapter(adapter);
}
我称之为
unitSpinner.setSelection(getIndex(unitSpinner, cur.getString(1)));
fromSpinner.setSelection(getIndex(fromSpinner, cur.getString(2)));
toSpinner.setSelection(getIndex(toSpinner, cur.getString(3)));
getIndex:
private int getIndex(Spinner spinner, String myString) {
int index = 0;
for (int i = 0; i < spinner.getCount(); i++) {
if (spinner.getItemAtPosition(i).toString().equals(myString)) {
index = i;
break;
}
}
return index;
}
fromSpinner和toSpinner适配器设置为新值,因为unitSpinner选择更改但是setSelection对它们不起作用,因为getIndex比较旧适配器并将index返回为0,除非我这样做:
unitSpinner.setSelection(getIndex(unitSpinner, cur.getString(1)));
new Handler().postDelayed(new Runnable() {
public void run() {
fromSpinner.setSelection(getIndex(fromSpinner, cur.getString(2)));
toSpinner.setSelection(getIndex(toSpinner, cur.getString(3)));
}
}, 1000);
这使我的应用程序显得很慢,因为我必须等待1000毫秒才能看到更改。设置较小的等待时间(如100毫秒)并不是每次都有效。我该怎么办?
答案 0 :(得分:0)
尝试:
fromSpinnerAdapter.notifdatachanged();