如何在RecyclerView中烘烤已选中复选框的数量?

时间:2018-02-20 12:21:13

标签: java android checkbox

如果活动加载时选中了所有复选框,或者未选中所有复选框,并且从该点开始选中或取消选中,但问题仍然存在实际上,活动可能会加载,并检查了几个复选框。

我认为密钥是count = 0;如果我可以在表单加载时将count设置为复选框的数量...

这就是我setOnClickListener(new View.OnClickListener() {的{​​{1}}中的内容,适用于所有复选框从选中或未选中位置开始的时间:

adapter

3 个答案:

答案 0 :(得分:1)

在您的recyclerView适配器中尝试此.. .. 公共静态int计数变量.. 并在onBindViewHolder()中将checkedChangeListener()添加到您的复选框

public static int count = 0; // field variable

 @Override
 public void onBindViewHolder(final ItemViewHolder holder, final int 
                                                           position)
  {


        holder.cb.setOnCheckedChangeListener(new 
                                  CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {


            if (isChecked)
            {
                count ++;
                Toast.makeText(context_type, "count is " + count, 
            Toast.LENGTH_SHORT).show();

            }
            else 
            {


                count --;

      Toast.makeText(context_type, "count is " + count, 
                             Toast.LENGTH_SHORT).show();
            }


        }
    });
 }
  

你也可以检查所有已检查或不检查..像这样...

if(count == 0)
 {
    //nothing checked
  }
else if(count == mList.size())
   {
      //all checked
    }
  

在您的适配器中不要忘记添加这些以记住已检查的位置: -

    @Override
    public int getItemViewType(int position) {


       return position;

     }

    @Override
    public long getItemId(int position) {


        return position;
    }

答案 1 :(得分:1)

获取所有视图并计算已选中的复选框:

int checked = 0;

for (int i = 0; i < rv.getChildCount(); i++) {

    try {
        CheckBox checkBox = rv.getChildAt(i).findViewById(R.id.checkbox);
        if (checkBox.isChecked()) checked++;
    } catch (NullPointerException e) {
        e.printStackTrace();
    }
}

Toast.makeText(this, "Checked: " + checked, Toast.LENGTH_SHORT).show();

其中rv是您的回收者视图,R.id.checkbox是您的复选框ID

它适用于我,但请确保适配器已为recyclerview创建了视图。

答案 2 :(得分:0)

感谢上面的帮助,但我找到了另一种简化代码的方法。很有用,因为我在许多活动中重复这个过程。

在我的活动中,我使用sharedpreferences从服务器中将已经选中的复选框放入Gson

       //Here, we PUT the arraylist into the sharedPreferences/*
SharedPreferences sharedPreferencescheckedContactsAsArrayList = PreferenceManager.getDefaultSharedPreferences(getApplication());
SharedPreferences.Editor editorcheckedContactsAsArrayList = sharedPreferencescheckedContactsAsArrayList.edit();
Gson gsoncheckedContactsAsArrayList = new Gson();
String jsoncheckedContactsAsArrayList = gsoncheckedContactsAsArrayList.toJson(checkedContactsAsArrayList);
                              editorcheckedContactsAsArrayList.putString("checkedContactsAsArrayList", jsoncheckedContactsAsArrayList);
editorcheckedContactsAsArrayList.commit();

在我的adapter我从sharedpreferences获取这些值,创建一个名为checkedContactsAsArrayList的arrayList:

//we are fetching the array list checkedContactsAsArrayList, created in the activity. with this we will put a tick in the checkboxes of the selected numbers
SharedPreferences sharedPreferencescheckedContactsAsArrayList = PreferenceManager.getDefaultSharedPreferences(context_type);
Gson gsoncheckedContactsAsArrayList = new Gson();
String jsoncheckedContactsAsArrayList = sharedPreferencescheckedContactsAsArrayList.getString("checkedContactsAsArrayList", "");
Type type2 = new TypeToken<ArrayList<String>>() {
 }.getType();
checkedContactsAsArrayList = gsoncheckedContactsAsArrayList.fromJson(jsoncheckedContactsAsArrayList, type2);

然后我的onBindViewHolder

   @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder viewHolder, final int position) {

etc...

 holder.check.setOnClickListener(new View.OnClickListener() {

                    @Override
                    //on checkbox click
                    public void onClick(View v) {

                        //pos is the row number that the clicked checkbox exists in
                        Integer pos = (Integer) holder.check.getTag();

                        //if the checkbox is checked
                        if (holder.check.isChecked())

                        {
                            //we want to add the phone number of the checked row into our arraylist.

                            //add the checked number into the arraylist
                            checkedContactsAsArrayList.add(theContactsList.get(pos).getPhone());

                        } else {
                            //remove the checked number from the arraylist
                            checkedContactsAsArrayList.remove(theContactsList.get(pos).getPhone());

                        }

同样在我的onBindViewHolder中,不需要loops或其他任何内容:

                if (checkedContactsAsArrayList.size() == 0) {

                    Toast.makeText(context_type, "count is 0!", Toast.LENGTH_SHORT).show();

                } else {

                    Toast.makeText(context_type, "The count is " + checkedContactsAsArrayList.size(), Toast.LENGTH_SHORT).show();

                }

谢谢!