我有联系人列表。我有一个复选框来选择联系人。我想在另一个列表中添加这些选定的联系人,称为邀请数组列表。如果选中了名为toogleContactsSelection的复选框,我创建了一种方法来添加邀请列表中的所有联系人。这是我在活动中使用的。
如果选中复选框,我创建了另一种在邀请arraylist中添加联系人的方法。
现在,如果未选中复选框,即复选框的onCheckChangeListener,我想从邀请数组列表中删除该对象。
但是对象没有从invitationArrayList中删除。
适配器:
man grep
这是一个活动的代码:
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);
}
}
}
}
现在我正在更新onSendInvites列表,但是当我检查或取消选中api时,它的行为不符合预期。
答案 0 :(得分:1)
好的,所以我说的很少有愚蠢的错误,
一个。替换
final Contact contact = contactArrayList.get(position);
与
final Contact contact = contactArrayList.get(holder.getAdapterPosition());
湾您正在添加类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);
℃。您正在尝试删除类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);
}
}
}
此处
contact.setSeletected(boolean status)
和contact.isSelected()
分别是setter和getter
答案 1 :(得分:-1)
而不是使用object删除使用索引来删除项目:
invitationArrayList.remove(position);
它会起作用。
您正在使索引超出范围,因为用户可能会在添加项目之前删除该项目。可能是您的invitationArrayList中不存在的特定元素。因此,请尝试此代码:
if (invitationArrayList!=null && invitationArrayList.size() > 0 && invitationArrayList.size() > position +1){
invitationArrayList.remove(position);
}