繁体   English   中英

多选ListView和SharedPreferences

[英]Multiple choice ListView and SharedPreferences

我想保存多选列表视图复选框的状态。 我有以下布局。

在此输入图像描述

我想要做的是保存例如“test1和test3”的状态,当我返回到此活动时,将选中此复选框。 我正在使用共享首选项。 我有以下代码。

这会加载我的列表:

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

这是以下代码加载并保存复选框的状态(据说)。

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

比我在按钮中加载SaveSelections和Clear Selections方法。

问题是这不起作用。 有人可以帮帮我吗?

致以我的问候。

我最近遇到了类似的问题。 这可能是因为ListView中的项目正在被回收(属性和所有)。 您应该明确设置未保存到未检查状态的那些。

编辑:另外,虽然我可能会误解,但我认为您不应该将保存的字符串添加到“数据”中,因为这是您的适配器用于生成列表的内容,不是吗?

尝试这个:

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

我希望这个对你有用!

暂无
暂无

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

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