Reputation: 21
I have an issue using a viewGroup
, for some reason the id
doesn't process the statement, and really I tried everything, can you help please?
I don't know if it is something about the version3.3.2
or what's wrong with the code
public class popularAdapter extends RecyclerView.Adapter<popularAdapter.ImageViewHolder> {
private Context mContext;
private List<popular> mPopulars;
public popularAdapter (Context context,List<popular>populars){
mContext=context;
mPopulars=populars;
}
@Override
public ImageViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
//HERE IS THE ISSUE
View v= LayoutInflater.from(mContext).inflate(R.layout.popular_item2,viewGroup,false);
return new ImageViewHolder(v);
}
@Override
public void onBindViewHolder(ImageViewHolder holder, int position) {
popular popularCur=mPopulars.get(position);
ImageViewHolder.prodName.setText(popularCur.getProduc_name());
}
@Override
public int getItemCount() {
return 0;
}
public class ImageViewHolder extends RecyclerView.ViewHolder {
public TextView prd_name;
public TextView prd_price;
public TextView prd_img;
public ImageViewHolder(View itemView) {
super(itemView);
prd_name =itemView.findViewById(R.id.prodName);
prd_price=itemView.findViewById(R.id.prodPrice);
prd_img =itemView.findViewById(R.id.prodImageHolder);
}
}
}
Upvotes: 2
Views: 118
Reputation: 4809
problem is here
you will never get any items in RecyclerView
because you are returning 0 in getItemCount
@Override
public int getItemCount()
{
//return 0 ?
return 0;
}
change with
@Override
public int getItemCount()
{
return mPopulars.size();
}
getItemCount
int getItemCount ()
Returns the total number of items in the data set held by the adapter.
Upvotes: 1
Reputation: 3505
Optimizing @Ashwin solanki answer
Change following code
@Override
public int getItemCount()
{
//return 0 ?
return 0;
}
to
@Override
public int getItemCount()
{
//return 0 ?
if(mPopulars != null) {
return mPopulars.size();
}
return 0;
}
Description: onCreateViewHolder and onBindViewHolder methods get called for the count which is returned by getItemCount method. As your code is always returning 0, these methods will not get called and list will not get populated on UI.
Also, check following line from you code from onBindViewHolder method:
ImageViewHolder.prodName.setText(popularCur.getProduc_name());
You are considering 'prodName' variable of 'ImageViewHolder' class as static but it is not. Instead it should be
holder.prodName.setText(popularCur.getProduc_name());
Upvotes: 0
Reputation: 1112
Try this solution
public ImageViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View v= LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.popular_item2,viewGroup,false);
return new ImageViewHolder(v);
}
Upvotes: 0