Android:如何保存多个已选中的复选框状态,甚至关闭并重新打开应用程序

时间:2016-11-02 12:39:35

标签: android

我使用带有复选框的自定义列表视图...每当我选择复选框项时,它都会被选中而没有任何问题...关闭并重新打开所选应用程序后,复选框项目显示为未选中... 。如何在重新打开应用程序后保存选中的复选框项目.​​...我知道可以使用共享首选项...这是代码

public class SelectUserAdapter extends BaseAdapter {

public List<SelectUser> _data;
private ArrayList<SelectUser> arraylist;
Context _c;
ViewHolder v;
//RoundImage roundedImage;
List<String> list;

public String blockedlist;

public SelectUserAdapter(List<SelectUser> selectUsers, Context context) {
    _data = selectUsers;
    _c = context;
    this.arraylist = new ArrayList<SelectUser>();
    this.arraylist.addAll(_data);
}

@Override
public int getCount() {
    return _data.size();
}

@Override
public Object getItem(int i) {
    return _data.get(i);
}

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


@Override
public View getView(int i, View convertView, ViewGroup viewGroup) {
    View view = convertView;
    if (view == null) {
        LayoutInflater li = (LayoutInflater) _c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        view = li.inflate(R.layout.contacts_diplay_row, null);
        Log.e("Inside", "here--------------------------- In view1");
    } else {
        view = convertView;
        Log.e("Inside", "here--------------------------- In view2");
    }

    v = new ViewHolder();

    v.title = (TextView) view.findViewById(R.id.name);
    v.check = (CheckBox) view.findViewById(R.id.check);
    v.phone = (TextView) view.findViewById(R.id.no);
    v.imageView = (ImageView) view.findViewById(R.id.pic);

    final SelectUser data = (SelectUser) _data.get(i);
    v.title.setText(data.getName());
   // v.check.setChecked(data.getCheckedBox());
    v.phone.setText(data.getPhone());

    final  SelectUser p = getProduct(i);
    v.check.setOnCheckedChangeListener(myCheckChangeList);
    v.check.setTag(i);
    v.check.setChecked(p.isSelected);

    // Set image if exists
    try {

        if (data.getThumb() != null) {
            v.imageView.setImageBitmap(data.getThumb());
        } else {
            v.imageView.setImageResource(R.drawable.ic_launcher);
        }
        // Seting round image
        Bitmap bm = BitmapFactory.decodeResource(view.getResources(), R.drawable.ic_launcher); // Load default image
        //roundedImage = new RoundImage(bm);
        //v.imageView.setImageDrawable(roundedImage);
    } catch (OutOfMemoryError e) {
        // Add default picture
        v.imageView.setImageDrawable(this._c.getDrawable(R.drawable.ic_launcher));
        e.printStackTrace();
    }

    Log.e("Image Thumb", "--------------" + data.getThumb());

    // Set check box listener android
    v.check.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            CheckBox checkBox = (CheckBox) view;
            if (checkBox.isChecked()) {
                data.setCheckedBox(true);

                Log.i("cdcd", "onItemClick: " +data.toString());
              } else {
                data.setCheckedBox(false);
            }
        }
    });

    view.setTag(data);
    return view;
}





    // Filter Class
public void filter(String charText) {
    charText = charText.toLowerCase(Locale.getDefault());
    _data.clear();
    if (charText.length() == 0) {
        _data.addAll(arraylist);
    } else {
        for (SelectUser wp : arraylist) {
            if (wp.getName().toLowerCase(Locale.getDefault())
                    .contains(charText)) {
                _data.add(wp);
            }
        }
    }
    notifyDataSetChanged();
}




  // for check box................
    SelectUser getProduct(int position) {
    return ((SelectUser) getItem(position));
}

ArrayList<SelectUser> getBox() {
    ArrayList<SelectUser> box = new ArrayList<SelectUser>();
    for (SelectUser p : arraylist) {
        if (p.isSelected)
            box.add(p);
    }
    return box;
}

CompoundButton.OnCheckedChangeListener myCheckChangeList = new CompoundButton.OnCheckedChangeListener() {
    public void onCheckedChanged(CompoundButton buttonView,
                                 boolean isChecked) {



        getProduct((Integer) buttonView.getTag()).isSelected = isChecked;

        String result = "999";
        for (SelectUser pp : getBox()) {
            if (pp.isSelected){

                result += "," +pp.phone;// PhoneNumberUtils.formatNumber(pp.phone)
                list = new ArrayList<String>();
                list.add(result);

            }
        }
        blockedlist = String.valueOf(list);

        Toast.makeText(_c, String.valueOf(blockedlist), Toast.LENGTH_LONG).show();


    }
};

static class ViewHolder {
    ImageView imageView;
    TextView title, phone;
    CheckBox check;
}

}

2 个答案:

答案 0 :(得分:2)

将复选框的值存储在首选项数据中,每次打开应用程序时都会从首选项中获取值。

答案 1 :(得分:0)

为每个复选框选择布尔值。