我在Listview中创建了一个复选框。但是,当我关闭应用程序并重新打开时,它不会保存我列出的项目的状态。
然而,这对我没有用,我似乎无法找到如何解决这个问题的答案。如果有人可以帮助我完成ListAdapter Activty的代码,如下所示,我们将不胜感激。
public class ListAdapter extends BaseAdapter {
Context ctx;
LayoutInflater lInflater;
ArrayList<Product> objects;
ListAdapter(Context context, ArrayList<Product> products) {
ctx = context;
objects = products;
lInflater = (LayoutInflater) ctx
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
return objects.size();
}
@Override
public Object getItem(int position) {
return objects.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
if (view == null) {
view = lInflater.inflate(R.layout.item, parent, false);
}
Product p = getProduct(position);
((TextView) view.findViewById(R.id.tvDescr)).setText(p.name);
((TextView) view.findViewById(R.id.tvPrice)).setText(p.price + "");
((ImageView) view.findViewById(R.id.ivImage)).setImageResource(p.image);
CheckBox cbBuy = (CheckBox) view.findViewById(R.id.cbBox);
cbBuy.setOnCheckedChangeListener(myCheckChangList);
cbBuy.setTag(position);
cbBuy.setChecked(p.box);
return view;
}
Product getProduct(int position) {
return ((Product) getItem(position));
}
ArrayList<Product> getBox() {
ArrayList<Product> box = new ArrayList<Product>();
for (Product p : objects) {
if (p.box)
box.add(p);
}
return box;
}
OnCheckedChangeListener myCheckChangList = new OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
getProduct((Integer) buttonView.getTag()).box = isChecked;
}
};
}
我不确定我做错了什么,或者我把它放错了地方,或者整个代码是不正确的。
这是使用共享偏好设置的已编辑解决方案版本:
public class ListAdapter extends BaseAdapter {
Context ctx;
LayoutInflater lInflater;
ArrayList<Product> objects;
ListAdapter(Context context, ArrayList<Product> products) {
ctx = context;
objects = products;
lInflater = (LayoutInflater) ctx
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
return objects.size();
}
@Override
public Object getItem(int position) {
return objects.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
if (view == null) {
view = lInflater.inflate(R.layout.item, parent, false);
}
final Product p = getProduct(position);
((TextView) view.findViewById(R.id.tvDescr)).setText(p.name);
((TextView) view.findViewById(R.id.tvPrice)).setText(p.price + "");
((ImageView) view.findViewById(R.id.ivImage)).setImageResource(p.image);
final CheckBox cbBuy = (CheckBox) view.findViewById(R.id.cbBox);
SharedPreferences settings = ctx.getSharedPreferences("data",ctx.MODE_PRIVATE);
boolean Checked = settings.getBoolean(p.name, false);
cbBuy.setChecked(Checked);
cbBuy.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(cbBuy.isChecked()==true){
SharedPreferences settings = ctx.getSharedPreferences("data",Context.MODE_PRIVATE);
settings.edit().putBoolean(p.name, true).commit();
Toast.makeText(ctx, "You Selected" + p.name, Toast.LENGTH_SHORT).show();
}else{
SharedPreferences settings = ctx.getSharedPreferences("data", Context.MODE_PRIVATE);
settings.edit().putBoolean(p.name, false).commit();
Toast.makeText(ctx, "You Deselected" +p.name, Toast.LENGTH_SHORT).show();
}
}
});{
return view;
}
}
答案 0 :(得分:1)
字段不是永久存储。如果要在启动期间保留数据,则必须将其保存在关闭状态并在启动时加载。