Android保存多个CheckBox的状态

时间:2014-03-26 10:09:45

标签: android

我有很多复选框,我想保存它们的状态并在退出后恢复它们。 这是我的listview适配器的代码:

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;
    }
}

日志显示所有0,任何想法?

2 个答案:

答案 0 :(得分:2)

我联系到你忘了在editor.commit

之后致电editor.put

答案 1 :(得分:1)

为了存储多个CheckBox的状态,你需要创建bean类(Setter&amp; Getter)并需要存储CheckBox的状态。

并将其保存为

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

要获得帮助,您可以查看一下,这个示例完全符合您的要求,即保存状态,但您需要根据您的要求进行更改。

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

希望这会对你有所帮助。