对象未从复选框的 onCheckedChangelistener 列表中删除

object is not getting removed from list onCheckedChange listener of check box

我有一个联系人列表。我有一个 select 联系人的复选框。我想将这些 selected 联系人添加到另一个名为邀请数组列表的列表中。我已经创建了一种方法,如果复选框被 selected 称为 toogleContactsSelection,则将所有联系人添加到邀请列表中。这是我在 activity.

中使用的

如果选中select复选框,我已经创建了另一种在邀请数组列表中添加联系人的方法。

现在我想在未选中复选框的情况下从邀请数组列表中删除对象,即复选框的 onCheckChangeListener。

但是对象没有从 invitationArrayList 中删除。

适配器:

     public class InviteAdapter extends RecyclerView.Adapter<InviteAdapter.MyViewHolder> {

        private ArrayList<Contact> contactArrayList;
        private Context mContext;
        public ArrayList<Invitation>  invitationArrayList = new ArrayList<>();

        public class MyViewHolder extends RecyclerView.ViewHolder {
            public TextView name;
            private CheckBox checkBox;

            public MyViewHolder(View view) {
                super(view);
                name = (TextView) view.findViewById(R.id.textContactName);
                checkBox = (CheckBox) view.findViewById(R.id.checkBox);

            }
        }

        public InviteAdapter(Context context, ArrayList<Contact> contactArrayList) {
            this.contactArrayList = contactArrayList;
            this.mContext = context;

        }

        @Override
        public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View itemView = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.invite_contact_item, parent, false);

            return new MyViewHolder(itemView);
        }

        @Override
        public void onBindViewHolder(MyViewHolder holder, final int position) {
            final Contact contact = contactArrayList.get(position);
            holder.name.setText(contact.getmFullName());

            holder.checkBox.setChecked(contact.getSelected());

            holder.checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton compoundButton, boolean b) {

                    if(b)
                    {
                        invite(contact);

                        Log.e("inviteList",String.valueOf(invitationArrayList.size()));
                    }
                    else {
                        invitationArrayList.remove(contact);

                        Log.e("inviteList",String.valueOf(invitationArrayList.size()));
                    }
                }
            });
        }

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

        }

        public void toggleContactsSelection( boolean isSelected ) {
            for( Contact contact : contactArrayList ) {
                contact.setSelected(isSelected);

                    invite(contact);

            }
            notifyDataSetChanged(); // OR you can use notifyItemRangeChanged - which ever suits your needs
        }

        public void invite(Contact contact)
        {

            SharedPreferences sharedpreferences = mContext.getSharedPreferences("UserId", Context.MODE_PRIVATE);

            String mUserId = sharedpreferences.getString("userId","");

            DateFormat df = new SimpleDateFormat("EEE, d MMM yyyy, HH:mm", Locale.ENGLISH);
            String date = df.format(Calendar.getInstance().getTime());

            Invitation invitation = new Invitation();

            invitation.setSender_id(mUserId);
            invitation.setDate(date);
            invitation.setInvitee_no(contact.getmMobileNo());
            invitation.setStatus("0");
            invitation.setUser_name(contact.getmUserName());

            invitationArrayList.add(invitation);

        }

        public void removeInvite(int position)
        {
            invitationArrayList.remove(position);
        }

        public ArrayList<Invitation> getArrayList(){
            return invitationArrayList;
        }

    }


What is going wrong?

EDIT:

This is an adpater :


   public class InviteAdapter extends RecyclerView.Adapter<InviteAdapter.MyViewHolder> {

    private ArrayList<Contact> contactArrayList;
    private Context mContext;
    public ArrayList<Invitation>  invitationArrayList = new ArrayList<>();

    public class MyViewHolder extends RecyclerView.ViewHolder {
        public TextView name;
        private CheckBox checkBox;

        public MyViewHolder(View view) {
            super(view);
            name = (TextView) view.findViewById(R.id.textContactName);
            checkBox = (CheckBox) view.findViewById(R.id.checkBox);

        }
    }

    public InviteAdapter(Context context, ArrayList<Contact> contactArrayList) {
        this.contactArrayList = contactArrayList;
        this.mContext = context;

    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.invite_contact_item, parent, false);

        return new MyViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(final MyViewHolder holder, final int position) {
        final Contact contact = contactArrayList.get(holder.getAdapterPosition());
        holder.name.setText(contact.getmFullName());

        holder.checkBox.setChecked(contact.getSelected());

        holder.checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {

                if(b)
                {
                    invite(contact);

                    Log.e("inviteList",String.valueOf(invitationArrayList.size()));
                }
                else {

                    contactArrayList.get(position).setSelected(false);
                 //   holder.checkBox.setChecked(false);
                  // updateInvites();
                    Log.e("inviteList",String.valueOf(invitationArrayList.size()));
                }
            }
        });
    }

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

    }
    public void setChecked()
    {
        for( Contact contact : contactArrayList ) {


        }
    }


    public void toggleContactsSelection( boolean isSelected ) {
        for( Contact contact : contactArrayList ) {
            contact.setSelected(isSelected);

                invite(contact);

        }
        notifyDataSetChanged(); // OR you can use notifyItemRangeChanged - which ever suits your needs
    }

    public void invite(Contact contact)
    {

        Invitation invitation = new Invitation();

            SharedPreferences sharedpreferences = mContext.getSharedPreferences("UserId", Context.MODE_PRIVATE);

            String mUserId = sharedpreferences.getString("userId", "");

            DateFormat df = new SimpleDateFormat("EEE, d MMM yyyy, HH:mm", Locale.ENGLISH);
            String date = df.format(Calendar.getInstance().getTime());

            invitation.setSender_id(mUserId);
            invitation.setDate(date);
            invitation.setInvitee_no(contact.getmMobileNo());
            invitation.setStatus("0");
            invitation.setUser_name(contact.getmUserName());
            invitation.setContact_id(contact.getContactId());

            invitationArrayList.add(invitation);

    }
    public ArrayList<Invitation> getArrayList(){
        return invitationArrayList;
    }

    public void updateInvites(){
        invitationArrayList.clear();
        for(Contact contact : contactArrayList){
            if(contact.getSelected()){

                invite(contact);
            }
        }
    }
}

这是 activity 的代码:

  sendInvites.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            mAdapter.updateInvites(); // updating list on sendInvites, 

            invitationArrayList = mAdapter.getArrayList();

            Log.e("inviteList",String.valueOf(invitationArrayList.size()));

            Gson gson = new Gson();
            String toServer = gson.toJson(
                    Collections.singletonMap("invitations", invitationArrayList)
            );

            new SendMultipleInvitesAsyncTask(InviteContactsActivity.this,InviteContactsActivity.this).execute(toServer);

            finish();
            Intent i = new Intent(InviteContactsActivity.this,InviteContactsActivity.class);
            i.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
            startActivity(i);
        }
    });

现在我正在更新 onSendInvites 列表,但是当我选中或取消选中 api 时,它没有按预期运行。

而不是使用对象删除使用索引删除项目:

   invitationArrayList.remove(position);

它会起作用。

您的索引越界,因为用户可能在添加项目之前删除了它。可能是您的 invitationArrayList 中不存在的特定元素。所以为此试试这个代码:

if (invitationArrayList!=null && invitationArrayList.size() > 0 && invitationArrayList.size() > position +1){
 invitationArrayList.remove(position);
}

好的,所以我说的是一些愚蠢的错误,

一个。替换

final Contact contact = contactArrayList.get(position);

final Contact contact = contactArrayList.get(holder.getAdapterPosition());

b。您正在添加 Class Invitation

的对象
Invitation invitation = new Invitation();
    invitation.setSender_id(mUserId);
    invitation.setDate(date);
    invitation.setInvitee_no(contact.getmMobileNo());
    invitation.setStatus("0");
    invitation.setUser_name(contact.getmUserName());

    invitationArrayList.add(invitation);

c。您正在尝试删除 Class Contacts

的对象
else {
                    invitationArrayList.remove(contact);

                    Log.e("inviteList",String.valueOf(invitationArrayList.size()));
                }

解决方案

在 onBindViewHolder

   holder.checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
            contactArrayList.get(holder.getAdapterPosition()).setSelected(b);
            Log.e("inviteList",String.valueOf(invitationArrayList.size()));
            }
        });

然后做一个类似

的方法
private void updateInvites(){
    invitationArrayList.clear();
    for(Contacts contacts : contactsArrayList){
        if(contacts.isSelected()){
             invite(contact);
        }
    }
}

here contact.setSeletected(boolean status) and contact.isSelected() are setters and getters respectively

编辑: Here is the link to my blog on RecyclerView which explains most of the concepts of Simple RecyclerView.