Multiple ListView and SharedPreferences

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

enter image description here

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() {
    // 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;
}

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.

+5
source share
1 answer

. , ListView ( ). , .

EDIT: , , , "", , , ?

:

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(), "");
        ArrayList<String> savedItemsList = (ArrayList<String>) 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 (savedItemList.contains(currentItem)) {
                this.mList.setItemChecked(i, true);
            } else {
                this.mList.setItemChecked(i, false);
            }
        }
    }
}

, !

+1

All Articles