我正在尝试在Android应用中实施持久检查清单,即使您关闭应用也会检查相同的项目。
出于某种原因,我无法保存。它会将所有内容重置为未选中状态,即使我已声明要检查第3项的默认值。
到目前为止,这是我的代码。我将数组存储在sharedprefs中,然后在需要时抓取它们。
适配器:
private class MyCustomAdapter extends ArrayAdapter<ListItemClass> {
private ArrayList<ListItemClass> listItemList;
public MyCustomAdapter(Context context, int textViewResourceId,
ArrayList<ListItemClass> itemList) {
super(context, textViewResourceId, itemList);
this.listItemList = new ArrayList<ListItemClass>();
this.listItemList.addAll(itemList);
}
private class ViewHolder {
CheckBox name;
TextView desc;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
//Log.v("ConvertView", String.valueOf(position));
if (convertView == null) {
LayoutInflater vi = (LayoutInflater) context.getSystemService(
Context.LAYOUT_INFLATER_SERVICE);
convertView = vi.inflate(R.layout.checklist_item, null);
holder = new ViewHolder();
holder.desc = (TextView) convertView.findViewById(R.id.code);
holder.name = (CheckBox) convertView.findViewById(R.id.taskListCheckBox);
holder.name.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
CheckBox cb = (CheckBox) v;
ListItemClass item = (ListItemClass) cb.getTag();
Toast.makeText(context, "Toggled: " + cb.getText(),
Toast.LENGTH_SHORT).show();
item.setSelected(cb.isChecked());
prefsEditor.putString(item.getCode(), item.getString()).commit();
ArrayList<ListItemClass> itemList = getArray();
listItemList.clear();
listItemList.addAll(itemList);
}
});
convertView.setTag(holder);
holder.name.setTag(listItemList.get(position));
}
else {
holder = (ViewHolder) convertView.getTag();
}
ListItemClass listItem = listItemList.get(position);
holder.desc.setText(" (" + listItem.getName() + ")");
holder.name.setText(listItem.getCode());
holder.name.setChecked(listItem.isSelected());
holder.name.setTag(listItem);
return convertView;
}
}
ListItemClass:
public class ListItemClass {
String name = null;
String desc = null;
boolean selected = false;
public ListItemClass(String name, String desc, boolean selected) {
super();
this.name = name;
this.desc = desc;
this.selected = selected;
}
public String getCode() {
return name;
}
public void setCode(String code) {
this.name = code;
}
public String getName() {
return desc;
}
public void setName(String name) {
this.desc = name;
}
public String getString() {
StringBuilder sb = new StringBuilder();
sb.append(this.getCode() + ",");
sb.append(this.getName() + ",");
sb.append(String.valueOf(this.isSelected()));
return sb.toString();
}
public boolean isSelected() {
return selected;
}
public void setSelected(boolean selected) {
this.selected = selected;
}
}
getarray()(从sharedprefs中获取):
private ArrayList<ListItemClass> getArray() {
ArrayList<ListItemClass> result = new ArrayList<ListItemClass>();
Map<String, ?> map = prefs.getAll();
if(!map.isEmpty()) {
for(Map.Entry<String, ?> entry:map.entrySet()) {
String itemValue = entry.getValue().toString();
String key = entry.getKey();
List<String> list = Arrays.asList(itemValue.split(","));
result = addCountry(result, key, list.get(1), Boolean.getBoolean(list.get(2)));
}
}
return result;
}
addCountry():
private ArrayList<ListItemClass> addCountry(ArrayList<ListItemClass> array, String name, String desc, Boolean selected) {
ListItemClass country = new ListItemClass(name, desc, selected);
prefsEditor.putString(country.getCode(), country.getString()).commit();
array.add(country);
return array;
}
编辑:这是我分配我的共享首选项的地方:
SharedPreferences prefs = context.getSharedPreferences("taskList", Context.MODE_PRIVATE);
SharedPreferences.Editor prefsEditor = prefs.edit();
答案 0 :(得分:0)