每次打开应用程序时,如何使多个复选框保持相同状态?

时间:2015-12-29 07:10:05

标签: android

这是我的适配器类 当我再次打开应用程序时,我必须再次单击复选框,我的数据在首选项中正确保存,并且它也进行了比较,但问题是它没有显示在for循环中的复选框上检查,任何帮助都将被评估

    @Override
public View getView(final int position, View convertView, ViewGroup parent) {

    //getting sharedpreference DATa here
    animals = PoJo.readList(context, "savingkeyforcheckbox");
    final RecordHolder holder;
    view = convertView;
    mInflater = (LayoutInflater) context
            .getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
    if (view == null) {
        view = mInflater.inflate(R.layout.items, parent, false);
        holder = new RecordHolder();
        holder.icon = (ImageView) view.findViewById(R.id.icon);
        holder.packagename = (TextView) view.findViewById(R.id.packagename);
        holder.clickpackage_checkbox = (CheckBox) view
                .findViewById(R.id.clickpackage_checkbox);
        view.setTag(holder);
    } else {
        holder = (RecordHolder) view.getTag();
    }
    holder.clickpackage_checkbox.setId(position);
    holder.packagename.setId(position);
    item = getItem(position);
    if (animals.size() > 0) {
        for (int i = 0; i < animals.size(); i++) {
            if (animals.get(i).equalsIgnoreCase(item.getLabel())) {
                Log.e("do check here", "CHECK");
                holder.clickpackage_checkbox.setChecked(true);
            } else {
                holder.clickpackage_checkbox.setChecked(false);

            }
        }
    } else {
        holder.clickpackage_checkbox.setChecked(false);
    }
    if (booleanArray.get(position)) {
        holder.clickpackage_checkbox.setChecked(true);
        item.setChecked(true);
    } else {
        holder.clickpackage_checkbox.setChecked(false);
        item.setChecked(false);
    }

    holder.packagename.setText(item.getLabel());
    Drawable icons = Utilities.createIconThumbnail(item.getIcon(), context);
    holder.icon.setImageDrawable(icons);
    holder.clickpackage_checkbox.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            if (holder.clickpackage_checkbox.isChecked()) {
                booleanArray.put(position, true);
            selecteditems.add(listofitems.get(position).getLabel());
            } else {
                booleanArray.put(position, false);
                selecteditems.remove(listofitems.get(position).getLabel());
            }
        }
    });
    //POJO IS MODEL CLASS AND SAVING PREFERENCES OF ITEMS IN WRITELIST
    PoJo.writeList(context, selecteditems, "savingkeyforcheckbox");
    SelectedAdapter.this.notifyDataSetChanged();
    return view;
}

1 个答案:

答案 0 :(得分:0)

您可以使用Getter和Setter来保存和检索首选项的复选框值  并将其设置为复选框。