Chcę zapisać stan pola wyboru listy wyboru z wieloma opcjami. Mam następujący układ.Wiele opcji ListView i SharedPreferences
co chcę zrobić, to zapisać stan, na przykład, "test1 i test3" i kiedy wrócę do tej działalności to checkboxs są sprawdzane. Używam wspólnych preferencji. Mam następujący kod.
Ten ładuje moją listę:
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();
a to następujące ładunki kod i zapisuje stan checkboxs (podobno).
@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;
}
Zamiast wczytać metody SaveSelections i Clear Selections w przyciskach.
Problem polega na tym, że to nie działa. Czy ktoś może mi pomóc?
Pozdrawiam.
Czy próbowałeś umieścić kilka logów w swoim kodzie? Na przykład, czy settingsActivity.contains (data.toString()) zwraca true? A jaki ciąg otrzymasz za pomocą metody getString()? – Egor
Witam. Problem polega na tym, że po ponownym wprowadzeniu tej czynności dodaję więcej wartości niż te, które istniały i wszystkie pola wyboru są prawdziwe. – unnamed
Następnie należy zastąpić istniejące wartości, oznacza to, że należy je przechowywać pod istniejącym kluczem. – Egor