Data binding in adapter of recyclerView - Android
Data binding in adapter of RecyclerView
- Android
public class DashboardPopularAdapter extends RecyclerView.Adapter<DashboardPopularAdapter.MyViewHolder> {
private Context mContext;
private List<PopularProduct> mPopularProductInfos;
public DashboardPopularAdapter(Context context, List<PopularProduct> popularProductInfos) {
mContext = context;
mPopularProductInfos = popularProductInfos;
}
@Override
public DashboardPopularAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent, int type) {
ViewDashboardProductsBinding viewProductCategoryBinding = ViewDashboardProductsBinding.inflate(LayoutInflater.from(parent.getContext()), parent, false);
return new MyViewHolder(viewProductCategoryBinding);
}
@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
final PopularProduct popularProductInfo = mPopularProductInfos.get(position);
holder.binding.txtProductName.setText(popularProductInfo.getProductName());
Picasso.with(mContext).load(popularProductInfo.getProductPhoto())
.error(R.drawable.icon_pills)
.into(holder.binding.imgProduct);
holder.binding.llProduct.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mContext.startActivity(new Intent(mContext, ProductDetailsActivity.class)
.putExtra("medicine_id", popularProductInfo.getProductId())
.putExtra("medicine_name", popularProductInfo.getProductName()));
}
});
@Override
public int getItemCount() {
return mPopularProductInfos.size();
}
public static class MyViewHolder extends RecyclerView.ViewHolder {
private final ViewDashboardProductsBinding binding;
public MyViewHolder(ViewDashboardProductsBinding binding) {
super(binding.getRoot());
this.binding = binding;
}
}
}
public class StudentAdapter extends RecyclerView.Adapter<StudentAdapter.ViewHolder> {
StudentListRowBinding listRowBinding;
ArrayList<Student> studentlist;
public StudentAdapter(ArrayList<Student> studentlist) {
this.studentlist = studentlist;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
listRowBinding= DataBindingUtil.inflate(LayoutInflater.from(parent.getContext()),R.layout.student_list_row,parent,false);
return new ViewHolder(listRowBinding.getRoot());
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
/*listRowBinding.stuname.setText(studentlist.get(position).getName());
listRowBinding.stumob.setText(studentlist.get(position).getMobile());
listRowBinding.stucity.setText(studentlist.get(position).getCity()); */
holder.stuname.setText(studentlist.get(position).getName());
holder.stumob.setText(studentlist.get(position).getMobile());
holder.stucity.setText(studentlist.get(position).getCity());
if (position == getItemCount() - 1)
holder.divider.setVisibility(View.GONE);
}
@Override
public int getItemCount() {
return studentlist.size();
}
class ViewHolder extends RecyclerView.ViewHolder {
TextView stuname, stumob, stucity;
View divider;
public ViewHolder(View itemView) {
super(itemView);
stuname = (TextView) itemView.findViewById(R.id.stuname);
stumob = (TextView) itemView.findViewById(R.id.stumob);
stucity = (TextView) itemView.findViewById(R.id.stucity);
divider = itemView.findViewById(R.id.divider);
}
}
}