2017-02-23 64 views
0

我是一名Android编程的初学者,我无法理解为什么在我的项目中的HomeAdapter类的onBindViewHolder实现上出现错误。我的onBindViewHolder在Android中的错误

***This my HomeAdapter.*** 

这是我的课程,我想用我的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); 
       } 
      }); 

     } 

我的custom_view。 强调文本

<?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> 

,这是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). 

请ASIST错误在这个问题上

+0

你在哪一行出错?你的textview没有找到,但代码是正确的。请告诉我线路上出现错误 –

+0

HomeAdapter.java:56 HomeAdapter的第56行是?请发送第56行代码 –

+0

@PETERSON,请您突出您的第56行代码?实际发生错误的地方。很难从这些大量的代码中找出答案。 –

回答

2

更改此holder.count.setText(products.getNumOfProducts());holder.count.setText(String.valueOf(products.getNumOfProducts()));。我猜products.getNumOfProducts()返回一个int值,android正在为该int值寻找一个资源值,并且找不到它。所以你应该给字符串值。

+0

谢谢..我工作 –

+0

我很高兴听到这一点。也许你应该给它正确的答案。谢谢 ;) – faranjit