Reputation: 127
I'm trying to implement RecyclerView in an app. I followed the android hive guide but the items won't show. After a lot of checks I still couldn't find the problem.
Do I need to use implementation RecylerView in my build.app I'm using androidX I'm using this RecylcerView in a fragment not activity
My adapter class:
public class CouponsAdapter extends RecyclerView.Adapter<CouponsAdapter.ViewHolder> {
private List<CouponsModel> couponsList;
public class ViewHolder extends RecyclerView.ViewHolder {
private TextView productIds,couponUsage,couponLimit,couponAmount,couponCode,couponType,date;
public ViewHolder(@NonNull View itemView) {
super(itemView);
productIds=(TextView)itemView.findViewById(R.id.products_ids);
couponUsage=(TextView)itemView.findViewById(R.id.usage_limit);
couponAmount=(TextView)itemView.findViewById(R.id.coupon_amount);
couponCode=(TextView)itemView.findViewById(R.id.coupon_code);
couponType=(TextView)itemView.findViewById(R.id.coupon_type);
date=(TextView)itemView.findViewById(R.id.date);
}
}
public CouponsAdapter (List<CouponsModel> couponsList){
this.couponsList=couponsList;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.coupons_list,parent,false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
CouponsModel couponsModel=couponsList.get(position);
holder.date.setText(couponsModel.getDate());
holder.couponType.setText(couponsModel.getCouponType());
holder.couponCode.setText(couponsModel.getCouponCode());
holder.couponAmount.setText(couponsModel.getCouponAmount());
holder.couponUsage.setText(couponsModel.getCouponUsage());
holder.productIds.setText(couponsModel.getProductIds());
}
@Override
public int getItemCount() {
if(couponsList.size() == 0)
return 1;
return couponsList.size();
}
My fragment :
// Inflate the layout for this fragment
View view= inflater.inflate(R.layout.fragment_coupons, container, false);
recyclerView=view.findViewById(R.id.coupons_recyler_view);
couponsAdapter=new CouponsAdapter(couponsModelList);
recyclerView.setAdapter(couponsAdapter);
int x=32;
couponsModelList.add(new CouponsModel(x,x,x,x,"free50","free",x));
couponsAdapter.notifyDataSetChanged();
return view;
}
Upvotes: 0
Views: 115
Reputation: 459
You are missing Layout-manager .Just add this line to your code.
recyclerView.setLayoutManager(new LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false));
Upvotes: 0
Reputation: 185
You should set Layout Manager to your RecyclerView.
If layout manager is not set, you will get the following error: No layout manager attached; Skipping layout
Upvotes: 2