简体   繁体   中英

Multiple choice ListView and SharedPreferences

I want to save the state of a multiple choice listview checkbox's. I have the following layout.

在此输入图像描述

What i want to do is to save the state of, for instance, "test1 and test3" and when i return to this activity this checkboxs are checked. I'm using shared preferences. 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 following code loads and saves the state of the checkboxs (supposedly).

@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 methods in buttons.

The problem is that this isn't working. can somebody help me please?

My regards.

I ran into a similar issue recently. It's likely because items in the ListView are being recycled (properties and all). You should explicitly set the ones that were NOT saved to the unchecked state.

EDIT: Also, though I may be misunderstanding, I don't think you should be adding your saved strings to "data" because that's what your adapter is using to generate your list, isn't it?

Try this:

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);
            }
        }
    }
}

I hope it works for you!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM