[英]Error on my onBindViewHolder in Android
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. 我是android编程的初学者,我无法理解为什么我的项目中HomeAdapter类的onBindViewHolder实现会出错。
***This my HomeAdapter.***
This my class which i want to use with my recyclerview and cardview. 这是我想与recyclerview和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. 我的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 这是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 请在这个问题上坚持
Change this holder.count.setText(products.getNumOfProducts());
更改此
holder.count.setText(products.getNumOfProducts());
to holder.count.setText(String.valueOf(products.getNumOfProducts()));
到
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. 我猜
products.getNumOfProducts()
返回一个int
值,而android正在寻找该int值的资源值,但找不到它。 So you should give string value. 因此,您应该提供字符串值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.