我想保存多选列表视图复选框的状态。我有以下布局。
我想要做的是保存例如“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方法。
问题是这不起作用。请有人帮帮我吗?
我的问候。
答案 0 :(得分:1)
我最近遇到了类似的问题。这可能是因为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);
}
}
}
}
我希望它适合你!