Tôi muốn lưu trạng thái của một hộp kiểm danh sách nhiều lựa chọn. Tôi có bố cục sau đây.Nhiều lựa chọn ListView và SharedPreferences
gì tôi muốn làm là để lưu trạng thái của, ví dụ, "test1 và Test3" và khi tôi quay trở lại hoạt động này checkboxs này được kiểm tra. Tôi đang sử dụng tùy chọn được chia sẻ. Tôi có đoạn mã sau.
này tải danh sách của tôi:
mList = (ListView) findViewById(R.id.ListViewTarefas);
final TarefaDbAdapter db = new TarefaDbAdapter(this);
db.open();
data = db.getAllTarefas(getIntent().getExtras().get("nomeUtilizadorTarefa").toString());
adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_multiple_choice,data);
mList.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
mList.setAdapter(adapter);
LoadSelections();
và đây là tải đoạn mã sau và lưu trạng thái của checkboxs (được cho là).
@Override
protected void onPause() {
// always handle the onPause to make sure selections are saved if user clicks back button
SaveSelections();
super.onPause();
}
private void ClearSelections() {
// user has clicked clear button so uncheck all the items
int count = this.mList.getAdapter().getCount();
for (int i = 0; i < count; i++) {
this.mList.setItemChecked(i, false);
}
// also clear the saved selections
SaveSelections();
}
private void LoadSelections() {
// if the selections were previously saved load them
SharedPreferences settingsActivity = getPreferences(MODE_PRIVATE);
if (settingsActivity.contains(data.toString())) {
String savedItems = settingsActivity.getString(data.toString(), "");
this.data.addAll(Arrays.asList(savedItems.split(",")));
int count = this.mList.getAdapter().getCount();
for (int i = 0; i < count; i++) {
String currentItem = (String) this.mList.getAdapter().getItem(i);
if (this.data.contains(currentItem)) {
this.mList.setItemChecked(i, true);
}
}
}
}
private void SaveSelections() {
// save the selections in the shared preference in private mode for the user
SharedPreferences settingsActivity = getPreferences(MODE_PRIVATE);
SharedPreferences.Editor prefEditor = settingsActivity.edit();
String savedItems = getSavedItems();
prefEditor.putString(data.toString(), savedItems);
prefEditor.commit();
}
private String getSavedItems() {
String savedItems = "";
int count = this.mList.getAdapter().getCount();
for (int i = 0; i < count; i++) {
if (this.mList.isItemChecked(i)) {
if (savedItems.length() > 0) {
savedItems += "," + this.mList.getItemAtPosition(i);
} else {
savedItems += this.mList.getItemAtPosition(i);
}
}
}
return savedItems;
}
Hơn tôi tải phương thức SaveSelections và Clear Selections trong các nút.
Vấn đề là điều này không hoạt động. ai đó có thể giúp tôi được không?
Trân trọng.
Bạn đã cố gắng đặt một số nhật ký trong mã của mình chưa? Ví dụ, hiện settingsActivity.contains (data.toString()) trả về true? Và String nào bạn nhận được thông qua phương thức getString()? – Egor
Xin chào. Vấn đề là khi tôi nhập lại vào hoạt động này, tôi thêm nhiều giá trị hơn so với những gì đã tồn tại và nó đặt tất cả các hộp kiểm đúng. – unnamed
Sau đó, bạn nên ghi đè lên các giá trị hiện có, có nghĩa là bạn nên lưu trữ chúng dưới khóa hiện có. – Egor