I have made a checkbox within the Listview. However, when I shut the app and reopen, it doesn't save the state of items that I have listed.
However this has not worked for me, and I cant seem to find an answer as to how to solve this problem. If anyone can help me through the code for ListAdapter Activty as shown below, it would be appreciated.
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;
}
};
}
I'm not sure if I did something wrong or if I put it in wrong place or if the whole code is incorrect in doing this.
This is the Edited Solution Version using Shared Preferences:
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;
}
}