If you are using RecyclerView you need to maintain states of each row, means if you are checking using a condition(i.e. if) at any stage of RecyclerView item(in recyclerview adapter class) then you need to handle else as well. Here is the sample RecyclerViewAdapter :

public class ContactsAdapter extends RecyclerView.Adapter<ContactsAdapter.ViewHolder> {
List<ViewHolder> holders = new ArrayList<ViewHolder>();
private ArrayList<ContactModel> arrayList = new ArrayList<>();
private Context context;
private LayoutInflater inflater;

public void clearAdapter() {
    arrayList.clear();
    notifyDataSetChanged();
}

public ContactsAdapter(Context context, ArrayList<ContactModel> arrayList) {
    this.context = context;
    this.arrayList = arrayList;
}

public void setList(ArrayList<ContactModel> listSearch) {
    this.arrayList = listSearch;
    notifyItemRangeChanged(0, listSearch.size());
}

@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
inflater = LayoutInflater.from(parent.getContext());
    View view = inflater.inflate(R.layout.custom_row_for_contacts, parent, false);
    ViewHolder viewHolder = new ViewHolder(view);
    holders.add(viewHolder);
    return viewHolder;
}

@Override
public void onBindViewHolder(final ViewHolder holder, final int position) {
    final ContactModel current = this.arrayList.get(position);
    holder.txtDriverName.setText(current.getName());
    holder.txtDriverPhone.setText(current.getPhone());
    if (current.getImgUrl().length() > 0) {
    String urlLicenceThumb = UrlEndPoints.parentUrl + current.getImgUrl();
        Glide.with(context).load(urlLicenceThumb).error(R.mipmap.ic_launcher).into(holder.imgDriver);
    } else {
       Glide.with(context).load(R.mipmap.ic_launcher).into(holder.imgDriver);
    }
}

public void delete(int position) {
    arrayList.remove(position);
    notifyItemRemoved(position);
}

@Override
public int getItemCount() {
    return (null != arrayList ? arrayList.size() : 0);
}

class ViewHolder extends RecyclerView.ViewHolder {
    private TextView txtDriverName, txtDriverPhone;
    private CircleImageView imgDriver;
    private Button btnInvite;
    private CheckBox chkAdd;

    public ViewHolder(View itemView) {
        super(itemView);
        chkAdd = (CheckBox) itemView.findViewById(R.id.chkAdd);
        imgDriver = (CircleImageView) itemView.findViewById(R.id.imgDriver);
        txtDriverName = (TextView)itemView.findViewById(R.id.txtDriverName);
        txtDriverPhone = (TextView)     itemView.findViewById(R.id.txtDriverPhone);
        btnInvite = (Button) itemView.findViewById(R.id.btnInvite);
    }
}

You may also like

Leave a Reply