Reputation: 11
I am a beginner in android programming and i cant understand why there is an error on onBindViewHolder implementation of the HomeAdapter class in my project.
***This my HomeAdapter.***
This my class which i want to use with my recyclerview and cardview.
public class HomeAdapter extends RecyclerView.Adapter<HomeAdapter.MyViewHolder> {
private Context mContext;
private List<Products> productsList;
public class MyViewHolder extends RecyclerView.ViewHolder {
public TextView title,count;
public ImageView thumbnail,overflow;
public MyViewHolder(View itemView) {
super(itemView);
title=(TextView)itemView.findViewById(R.id.list_item);
count=(TextView)itemView.findViewById(R.id.list_count);
thumbnail=(ImageView) itemView.findViewById(R.id.thumbnail);
overflow=(ImageView) itemView.findViewById(R.id.overflow);
}
}
public HomeAdapter(Context mContext,List<Products> productsList){
this.mContext=mContext;
this.productsList=productsList;
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView= LayoutInflater.from(parent.getContext()).inflate(R.layout.custom_view,parent,false);
return new MyViewHolder(itemView);
}
@Override
public void onBindViewHolder(final MyViewHolder holder, int position) {
Products products=productsList.get(position);
holder.title.setText(products.getName());
holder.count.setText(products.getNumOfProducts());
Glide.with(mContext).load(products.getThumbnail()).into(holder.thumbnail);
holder.overflow.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
showPopUpMenu(holder.overflow);
}
});
}
my custom_view. emphasized text
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_margin="5dp"
android:id="@+id/card_view"
android:elevation="3dp"
card_view:cardCornerRadius="0dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/thumbnail"
android:layout_width="match_parent"
android:layout_height="160dp"
android:background="?attr/selectableItemBackgroundBorderless"
android:clickable="true"
android:scaleType="fitXY"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/list_item"
android:layout_below="@+id/thumbnail"
android:paddingLeft="10dp"
android:paddingTop="10dp"
android:paddingRight="10dp"
android:textSize="15dp"
android:textColor="#8E00aa"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/list_item"
android:id="@+id/list_count"
android:textSize="12dp"
android:paddingLeft="10dp"
android:paddingBottom="5dp"
android:paddingRight="10dp"
/>
<ImageView
android:layout_width="20dp"
android:layout_height="30dp"
android:id="@+id/overflow"
android:layout_alignParentRight="true"
android:layout_below="@+id/thumbnail"
android:layout_marginTop="10dp"
android:scaleType="centerCrop"
android:src="@drawable/navbar"/>
</RelativeLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
and this is the error on onBindViewHolder
FATAL EXCEPTION: main
Process: munene.com.barberbeautyapp, PID: 22033
android.content.res.Resources$NotFoundException: String resource ID #0xd
at android.content.res.Resources.getText(Resources.java:528)
at android.widget.TextView.setText(TextView.java:4406)
at munene.com.barberbeautyapp.HomeAdapter.onBindViewHolder(HomeAdapter.java:56)
at munene.com.barberbeautyapp.HomeAdapter.onBindViewHolder(HomeAdapter.java:23).
please asist on this problem
Upvotes: 0
Views: 874
Reputation: 1637
Change this holder.count.setText(products.getNumOfProducts());
to holder.count.setText(String.valueOf(products.getNumOfProducts()));
. I guess products.getNumOfProducts()
returns an int
value and android is looking for a resource value for that int value and can not find it. So you should give string value.
Upvotes: 2