1

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;

    }

}
77rshah
  • 29
  • 6
  • Use RecyclerView instead of ListView it's way better in term of performance and data persistance. – Abdennacer Lachiheb May 27 '17 at 19:43
  • @AbdenaceurLichiheb Im jsut trying to achieve the saving of the state at the moment, im still learning how to program on Android as I go along. – 77rshah May 27 '17 at 21:41

1 Answers1

1

Fields are not persistent storage. If you want to persist data across launches, you'll have to save it on close and load it on start.

F43nd1r
  • 7,690
  • 3
  • 24
  • 62
  • How would I implement that? – 77rshah May 27 '17 at 19:16
  • 1
    e.g. using SharedPreferences https://developer.android.com/training/basics/data-storage/shared-preferences.html – F43nd1r May 27 '17 at 19:17
  • Ok I had a look at that but im not sure how to implement this, and does it work on exit of the app and load back up of the app/ – 77rshah May 27 '17 at 19:24
  • This snippet should serve as an example https://stackoverflow.com/a/31065706/4388512 – F43nd1r May 27 '17 at 19:30
  • Alright so I have tried to give this a go, however of course my listview is being presented as a String, I can change things but not sure what im doing wrong. Would it be a better idea for me to save the activity state? – 77rshah May 27 '17 at 19:53
  • you cannot save a listview, you have to save the data it represents. (in your code `product`) – F43nd1r May 27 '17 at 21:02
  • I am having another problem, when I scroll it deselects the items – 77rshah May 27 '17 at 23:47