xXJJJasonMokXx
xXJJJasonMokXx

Reputation: 367

Android save states of multiple CheckBoxes

I have a lot of checkboxes which I want to save their states and restore them after exit. Here's the code for my listview adapter:

public class ListViewAdapter extends BaseAdapter {

    Context context;
    AppPicker.Package[] packagesForAdapter;
    SharedPreferences sharedPreferences;
    SharedPreferences.Editor editor;
    private Intent blackListIntent;
    List<Model> list = new ArrayList<Model>();
    ArrayList<Integer> checkBoxPositions = new ArrayList<Integer>();

    public ListViewAdapter(Context context, AppPicker.Package packages[], List<Model> list) {
        this.context = context;
        this.packagesForAdapter = packages;
        this.list = list;
        sharedPreferences = context.getSharedPreferences("CHECKBOXES", Context.MODE_PRIVATE);
        editor = sharedPreferences.edit();
        int count = packagesForAdapter.length;
        for(int i = 0; i < count; i++) {
            checkBoxPositions.add(sharedPreferences.getInt("checkBoxPosition_" + i, 0));
        }
        for(int i = 0; i < count; i++) {
            Log.d("CheckBox positions", checkBoxPositions.get(i).toString());
        }
    }

    @Override
    public int getCount() {
        return packagesForAdapter.length;
    }

    @Override
    public Object getItem(int i) {
        return null;
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    static class ViewHolder {
        TextView text;
        ImageView icon;
        CheckBox checkBox;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        if(convertView == null) {
            convertView = layoutInflater.inflate(R.layout.list_item, null);
            viewHolder = new ViewHolder();
            viewHolder.text = (TextView) convertView.findViewById(R.id.icon_text);
            viewHolder.icon = (ImageView) convertView.findViewById(R.id.icon_image);
            viewHolder.checkBox = (CheckBox) convertView.findViewById(R.id.icon_check);

            convertView.setTag(viewHolder);
        }else{
            viewHolder = (ViewHolder) convertView.getTag();
        }


        viewHolder.checkBox.setTag(position);

        viewHolder.icon.setImageDrawable(packagesForAdapter[position].icon);
        viewHolder.text.setText(packagesForAdapter[position].label);
        viewHolder.checkBox.setChecked(list.get(position).isSelected());
        viewHolder.checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean isCheck) {
                if(isCheck) {
                    int getPosition = (Integer) compoundButton.getTag();
                    list.get(getPosition).setSelected(compoundButton.isChecked());
                    editor.putInt("checkBoxPosition_" + getPosition, getPosition);
                    blackListIntent = new Intent("com.jason.floating.notification.appPickerFragment.ListViewAdapter");
                    blackListIntent.putExtra("packageName", packagesForAdapter[position].name);
                }else{
                    int getPosition = (Integer) compoundButton.getTag();
                    list.get(getPosition).setSelected(compoundButton.isChecked());
                    //editor.remove("checkBoxPosition_" + getPosition);
                    blackListIntent.removeExtra("packageName");
                }
                context.sendBroadcast(blackListIntent);
            }
        });

        return convertView;
    }
}

Log shows all 0s, any idea?

Upvotes: 1

Views: 536

Answers (2)

Amit Gupta
Amit Gupta

Reputation: 8939

In order to store the state of multiple CheckBox , you need to create bean class(Setter & Getter) and need to store the state of CheckBox.

And save it like

OnCheckedChangeListener myCheckChangList = new OnCheckedChangeListener() {
        public void onCheckedChanged(CompoundButton buttonView,
                boolean isChecked) {
            //Update here the state so thet you can retrieve it on getView() method
        }
    };

For Help you can have a look into this, this example exactly do what you want i.e it will save the state but you need to change according to ur requirement.

http://amitandroid.blogspot.in/2013/03/android-listview-with-checkbox-and.html

Hope this will help you.

Upvotes: 1

AlexMasca
AlexMasca

Reputation: 403

It seams to me that you forgot to call editor.commit after editor.put

Upvotes: 2

Related Questions