简体   繁体   English

多选ListView和SharedPreferences

[英]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. 我想要做的是保存例如“test1和test3”的状态,当我返回到此活动时,将选中此复选框。 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. 比我在按钮中加载SaveSelections和Clear Selections方法。

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). 这可能是因为ListView中的项目正在被回收(属性和所有)。 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! 我希望这个对你有用!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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