Đây là giải pháp của tôi.
Tôi cần phải bỏ qua sự kiện lựa chọn mục đầu tiên vì có sự phụ thuộc giữa Tuyến đường cấp Spinner và Hộp kiểm tuyến.
Và tất cả các điều khiển của tôi đều được thiết lập dựa trên lượt truy cập trước đó vào hoạt động.
// Used to count the number of times the onItemSelected gets fired
private int mGradeSelectionCount = 0;
private void attachHandlers() {
OnItemSelectedListener gradeRangeSelectionMadeListener;
gradeRangeSelectionMadeListener = new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapter, View view, int position, long id) {
// If the counter is 0 then we can assume that it is android firing the event
if (mGradeSelectionCount++ < 1) {
return;
}
if (mCmbGradeFrom.getSelectedItemPosition() == 0) {
// Uncheck the Route checkbox
mChkTypeRoute.setChecked(false);
} else {
// Check the Route checkbox
mChkTypeRoute.setChecked(true);
}
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// Dont care, keep the same values as before
}
};
mCmbGradeFrom.setOnItemSelectedListener(gradeRangeSelectionMadeListener);
mChkTypeRoute.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (!isChecked) {
mCmbGradeFrom.setSelection(0);
mCmbGradeTo.setSelection(0);
}
}
});
}
Tôi gặp vấn đề khác với người nghe. nếu tôi kích hoạt một nhiệm vụ async từ phương thức OnItemSelected, và sau đó xoay getLastNonConfigurationInstance sẽ rút lại null cho tác vụ tôi lưu tại onRetainNonConfigurationInstance. Nhưng nếu tôi đặt mã bên ngoài trong oncreate nó sẽ làm việc tốt .... – Maxrunner
bạn đã không nói với bạn đang chỉ cho chúng tôi 2 câu trả lời của bạn quá. – cosmincalistru
@cosmincalistru: Đã sửa lỗi liên kết - Tôi vừa sao chép đã dán nội dung vào URL trình duyệt của mình vào thời điểm đó, không nhận ra đó là câu trả lời thay vì câu hỏi. Có lẽ, trong thời gian, tôi sẽ đến để tha thứ cho bản thân mình. –