对象未从列表onCheckedChange监听器复选框中删除 [英] object is not getting removed from list onCheckedChange listener of check box
问题描述
我有一个联系人列表。我有一个复选框来选择联系人。这些选择的联系人我想添加在另一个名为邀请数组列表的列表中。我已创建一个方法来添加邀请列表中的所有联系人(如果选中了作为toogleContactsSelection调用的复选框)。
如果选中了复选框,我已创建了另一种在邀请函数列表中添加联系人的方法。
现在,如果未选中复选框,则要从邀请数组列表中删除该对象,即复选框的onCheckChangeListener。
但是对象未被删除invitationArrayList。
适配器:
public class InviteAdapter extends RecyclerView.Adapter ; InviteAdapter.MyViewHolder> {
私有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){
查看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);
邀请(联系);
}
notifyDataSetChanged(); //或者你可以使用notifyItemRangeChanged - 它适合你的需要
}
public void invite(联系人联系人)
{
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());
invitationArrayList.add(invitation);
}
public void removeInvite(int position)
{
invitationArrayList.remove(position);
}
public ArrayList< Invitation> getArrayList(){
return invitationArrayList;
}
}
出了什么问题?
编辑:
这是一个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){
查看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);
邀请(联系);
}
notifyDataSetChanged(); //或者你可以使用notifyItemRangeChanged - 它适合你的需要
}
public void invite(联系人联系人)
{
邀请邀请= new邀请();
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 inviteArrayList;
}
public void updateInvites(){
invitationArrayList.clear();
for(联系人联系人:contactArrayList){
if(contact.getSelected()){
invite(contact);
}
}
}
}
这里是一个活动的代码:
sendInvites.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View view){
mAdapter.updateInvites(); //更新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时, 。
确定,所以我说的几个蠢错误,
a。替换
final Contact contact = contactArrayList.get(position);
final联系contact = contactArrayList.get(holder.getAdapterPosition());
b。您正在添加邀请对象
邀请邀请=新邀请);
invitation.setSender_id(mUserId);
invitation.setDate(date);
invitation.setInvitee_no(contact.getmMobileNo());
invitation.setStatus(0);
invitation.setUser_name(contact.getmUserName());
invitationArrayList.add(invitation);
c。您尝试删除联系人
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();
(联系人联系人:contactsArrayList){
if(contacts.isSelected()){
invite(contact);
}
}
}
contact.setSeletected(boolean status)
和contact.isSelected()
分别是setter和getter
编辑:
这里是我的博客在RecyclerView的链接,解释了Simple RecyclerView的大部分概念。
I have a list of contacts. I have a check box to select the contacts. These selected contacts I want to add in another list called invitation array list. I have created a method to add all contacts in invitation list if check box is selected called as toogleContactsSelection. This I am using in an activity.
I have created another method to add contacts in invitation arraylist if check box is selected.
Now I want to remove the object from an invitation array list if check box is unchecked i.e onCheckChangeListener of check box.
But object is not getting removed from invitationArrayList.
Adapter:
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);
}
}
}
}
And here is an activity's code:
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);
}
});
Now I am updating list onSendInvites, but when I check or uncheck the api it dose not behave as expected.
OK, so there are few silly mistakes I say,
a. Replace
final Contact contact = contactArrayList.get(position);
with
final Contact contact = contactArrayList.get(holder.getAdapterPosition());
b. You are adding Object of 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. You are trying to remove object of Class Contacts
else {
invitationArrayList.remove(contact);
Log.e("inviteList",String.valueOf(invitationArrayList.size()));
}
Solution
in 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()));
}
});
then make a method like
private void updateInvites(){
invitationArrayList.clear();
for(Contacts contacts : contactsArrayList){
if(contacts.isSelected()){
invite(contact);
}
}
}
here
contact.setSeletected(boolean status)
andcontact.isSelected()
are setters and getters respectively
这篇关于对象未从列表onCheckedChange监听器复选框中删除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!