In my Listview
this code works:
for (int number = 0; number < matchingContacts.size(); number++) {
//if a phone number is in our array of matching contacts
if (matchingContacts.contains(selectPhoneContact.getPhone()))
{
//if a matching contact, no need to show the Invite button
viewHolder.invite.setVisibility(View.GONE);
//once a matching contact is found, no need to keep looping x number of time, move onto next contact
break;
} else {
//if not a matching contact, no need to show the check box
viewHolder.check.setVisibility(View.GONE);
}
}
If a phone number is in the matching
arraylist then it should make the invite button
invisible, if it is not in the matching
arraylist it should make the checkbox
invisible.
But not in my recyclerview
, in which I am trying to make the code work.
On first load it looks ok but as soon as you start to scroll the views get messed up - checkboxes and buttons appear where they are not supposed to.
I've read that in Recyclerview you are supposed to implement this with case statements
, and I've looked here Why RecyclerView items disappear with scrolling and here How to create RecyclerView with multiple view type? but for the life of me I cannot get it to work!
Can you help?
Here is my code:
public class PopulistoContactsAdapter extends RecyclerView.Adapter<PopulistoContactsAdapter.ViewHolder> {
//make a List containing info about SelectPhoneContact objects
public List<SelectPhoneContact> theContactsList;
Context context_type;
ArrayList<String> matchingContacts = new ArrayList<String>();
public static class ViewHolder extends RecyclerView.ViewHolder {
//In each recycler_blueprint show the items you want to have appearing
public TextView title, phone;
public CheckBox check;
public Button invite;
public ViewHolder(final View itemView) {
super(itemView);
//title is cast to the name id, in recycler_blueprint,
//phone is cast to the id called no etc
title = (TextView) itemView.findViewById(R.id.name);
phone = (TextView) itemView.findViewById(R.id.no);
invite = (Button) itemView.findViewById(R.id.btnInvite);
check = (CheckBox) itemView.findViewById(R.id.checkBoxContact);
}
}
public PopulistoContactsAdapter(List<SelectPhoneContact> selectPhoneContacts, Context context, int activity) {
theContactsList = selectPhoneContacts;
context_type = context;
matchingContacts.add("+3531234567");
matchingContacts.add("+3536789012");
matchingContacts.add("+3530987654");
matchingContacts.add("+3538765432");
}
@Override
public PopulistoContactsAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
Context context = parent.getContext();
LayoutInflater inflater = LayoutInflater.from(context);
View contactView = inflater.inflate(R.layout.recycler_blueprint, parent, false);
ViewHolder viewHolder = new ViewHolder(contactView);
return viewHolder;
}
@Override
public void onBindViewHolder(final PopulistoContactsAdapter.ViewHolder viewHolder, final int position) {
//bind the views into the ViewHolder
//selectPhoneContact is an instance of the SelectPhoneContact class.
//We will assign each row of the recyclerview to contain details of selectPhoneContact:
//The number of rows will match the number of contacts in our contacts list
final SelectPhoneContact selectPhoneContact = theContactsList.get(position);
//a text view for the name, set it to the matching selectPhoneContact
TextView title = viewHolder.title;
title.setText(selectPhoneContact.getName());
//a text view for the number, set it to the matching selectPhoneContact
TextView phone = viewHolder.phone;
phone.setText(selectPhoneContact.getPhone());
Button invite = viewHolder.invite;
CheckBox check = viewHolder.check;
for (int number = 0; number < matchingContacts.size(); number++) {
//if a phone number is in our array of matching contacts
if (matchingContacts.contains(selectPhoneContact.getPhone()))
{
//if a matching contact, no need to show the Invite button
viewHolder.invite.setVisibility(View.GONE);
//once a matching contact is found, no need to keep looping x number of time, move onto next contact
break;
} else {
//if not a matching contact, no need to show the check box
viewHolder.check.setVisibility(View.GONE);
}
}
}
@Override
public int getItemCount() {
return theContactsList.size();
}
}