简体   繁体   English

即使首次选中,然后取消选中,也会删除Recycler View项目

[英]Recycler View item deleted even if its first checked and then Unchecked

Multiple items in my recycler view can be checked and deleted by adding them to a integer array list for position and string array list for phone number. 通过将其添加到整数数组列表中的电话号码的位置和字符串数组列表,可以检查和删除我的Recycler视图中的多个项目。 They are added when the isChecked() property of the checkbox is true. 当复选框的isChecked()属性为true时,将添加它们。

    holder.delCheckbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            objCheck.setSelected(isChecked);
            holder.delCheckbox.setChecked(objCheck.getSelected());
            if (holder.delCheckbox.isChecked()) {

                arrPos.add(holder.getAdapterPosition());

                arrNo.add(arrSuitcaseContacts.get(holder.getAdapterPosition()).number);
            }

But if I uncheck an item, the item will remain added in the arraylist and will be deleted by the click of the button. 但是,如果我取消选中一个项目,该项目将继续添加到arraylist中,并将通过单击按钮删除。

I just want to delete only those items which are checked finally. 我只想删除最后检查的那些项目。 How do I do that? 我怎么做?

For example in the image Sam will also be deleted along with the Geeta because I checked Sam and unchecked it afterwords. 例如,在图像中,Sam也将与Geeta一起被删除,因为我检查了Sam并且在之后取消选中它。

在此输入图像描述

ContactsDeleteAdapter.java ContactsDeleteAdapter.java

public class ContactsDeleteAdapter extends RecyclerView.Adapter<ContactsDeleteAdapter.ViewHolder> {
    Context context;
    ArrayList<SuitcaseContacts> arrSuitcaseContacts;
    ArrayList<String> arrNo = new ArrayList<>();
    ArrayList<Integer> arrPos = new ArrayList<>();
    DBHelper dbHelper;


    public ContactsDeleteAdapter(Context context, ArrayList<SuitcaseContacts> arrSuitcaseContacts) {
        this.context = context;
        this.arrSuitcaseContacts = arrSuitcaseContacts;
    }


    @NonNull
    @Override
    public ContactsDeleteAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        {
            return new ViewHolder(LayoutInflater.from(context).inflate(R.layout.delete_contact_list, parent, false));
        }
    }

    @Override
    public void onBindViewHolder(@NonNull final ContactsDeleteAdapter.ViewHolder holder, final int position) {
        final SuitcaseContacts objCheck = arrSuitcaseContacts.get(holder.getAdapterPosition());

        holder.image.setImageResource(arrSuitcaseContacts.get(holder.getAdapterPosition()).image);
        holder.name.setText(arrSuitcaseContacts.get(holder.getAdapterPosition()).name);
        holder.number.setText(arrSuitcaseContacts.get(holder.getAdapterPosition()).number);


        holder.delCheckbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                objCheck.setSelected(isChecked);
                holder.delCheckbox.setChecked(objCheck.getSelected());
                if (holder.delCheckbox.isChecked()) {

                    arrPos.add(holder.getAdapterPosition());

                    arrNo.add(arrSuitcaseContacts.get(holder.getAdapterPosition()).number);
                }

                /*if (!holder.delCheckbox.isChecked()){

                    arrPos.remove(position);
                    arrNo.remove(position);
                }*/

                Log.d("CHKS", String.valueOf(holder.delCheckbox.isChecked()));
                Log.d("Selected no", arrNo.toString());
            }
        });


    }

    public void DeleteContacts(ArrayList<String> phone, ArrayList<Integer> arrPos) {
        Collections.sort(arrPos, Collections.reverseOrder());

        for (int i = 0; i < arrPos.size(); i++) {
            Log.d("arrPos", arrPos.get(i).toString());

            arrSuitcaseContacts.remove(arrPos.get(i));
        }

        dbHelper = DBHelper.getDB(context);

        if (!dbHelper.checkDB()) {
            dbHelper.createDB(context);
        }

        dbHelper.openDB();

        String[] phoneArr = phone.toArray(new String[phone.size()]);

        dbHelper.deleteMulContacts(phoneArr);

        Toast.makeText(context, "Contacts Deleted Successfully!", Toast.LENGTH_SHORT).show();


    }

    @Override
    public int getItemCount() {
        return arrSuitcaseContacts.size();
    }

    void delClickActivator() {
        DeleteContacts(arrNo, arrPos);

    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        ImageView image;
        TextView name, number;
        CheckBox delCheckbox;


        public ViewHolder(View itemView) {
            super(itemView);

            image = itemView.findViewById(R.id.imgContact);
            name = itemView.findViewById(R.id.txtName);
            number = itemView.findViewById(R.id.txtNumber);
            delCheckbox = itemView.findViewById(R.id.delCheckbox);


        }
    }
}


  [1]: https://i.stack.imgur.com/p94dH.jpg

Try to use commented lines with small changes: 尝试使用带有小变化的注释行:

if (holder.delCheckbox.isChecked()) {
  arrPos.add(holder.getAdapterPosition());
  arrNo.add(arrSuitcaseContacts.get(holder.getAdapterPosition()).number);
} else {
  arrPos.remove(holder.getAdapterPosition());
  arrNo.remove(arrSuitcaseContacts.get(holder.getAdapterPosition()).number);
}

you have to use your item position , and add it to a list (selectedContactToDelete) whenever checkbox is checked and then remove all the items which their position is among selectedContactToDelete from your arrSuitcaseContacts , and then apply a NotifyDataSetChanged on your adapter 您必须使用项目位置,并在选中复选框时将其添加到列表(selectedContactToDelete),然后从arrSuitcaseContacts中删除其位置在selectedContactToDelete中的所有项目,然后在adapter上应用NotifyDataSetChanged

and reversely , if you uncheck an element remove it from the list (selectedContactToDelete). 反之,如果取消选中一个元素,则将其从列表中删除(selectedContactToDelete)。

I solved this issue. 我解决了这个问题。 The key point was to remove the elements from arrPos and arrNo which were added after selecting a checkbox item BUT they must be removed using an INDEX represented by an int counter which here is addCounter 关键点是从arrPos和arrNo中删除选中复选框项后添加的元素但是必须使用由int计数器表示的INDEX删除它们,这里是addCounter

 if (holder.delCheckbox.isChecked()) {

                arrPos.add(holder.getAdapterPosition());
                arrNo.add(arrSuitcaseContacts.get(holder.getAdapterPosition()).number);
                addCounter++;
                }

What I did was initialized a static addCounter to -1 and whenever a checkbox item is selected, addCounter increments by ++ so for example if a first item is selected using a checkbox the arrPos and arrNo will add items to the 0th and the addCounter will increment by ++ from -1 and will be 0 as well 我所做的是将静态addCounter初始化为-1,每当选择一个复选框项时,addCounter以++为增量,例如,如果使用复选框选择第一项,则arrPos和arrNo将向第0项添加项目,addCounter将从-1开始递增+,也将为0

And whenever the item is unchecked from checbox it will be removed using the index represented by addCounter : 每当从checbox中取消选中该项时,它将使用addCounter表示的索引删除:

if (!holder.delCheckbox.isChecked()) {
                arrPos.remove(addCounter);
                arrNo.remove(addCounter);
                addCounter--;

            }

            if (!holder.delCheckbox.isChecked()) {
                arrPos.remove(addCounter);
                arrNo.remove(addCounter);
                addCounter--;

            }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM