I want to save the state of multiple selection listview. I have the following layout.

What I want to do is save the state, for example, “test1 and test3”, and when I return to this action, these checkboxes will be checked. I use general settings. I have the following code.
This loads my 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();
and this is the next code download and saves the state of the flags (presumably).
@Override
protected void onPause() {
SaveSelections();
super.onPause();
}
private void ClearSelections() {
int count = this.mList.getAdapter().getCount();
for (int i = 0; i < count; i++) {
this.mList.setItemChecked(i, false);
}
SaveSelections();
}
private void LoadSelections() {
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() {
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;
}
Than I load the SaveSelections and Clear Selections buttons in the buttons.
The problem is that this does not work. can someone help me?
Sincerely.
source
share