2017-07-20 56 views
1

我是新来的android我已经创建回收视图与其适配器,但它不工作。检查下面的代码。回收视图适配器不加载项目

我试图调试此活动,但没有错误。并且在OtherOfferingAdapter程序不在getItemCount()之后执行。

文件OtherOfferingAdapter.java

public class OtherOfferingAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> { 

    private Context context; 
    private LayoutInflater inflater; 
    List<OtherOfferingData> data = Collections.emptyList(); 
    OtherOfferingData current; 
    int currentPos=0; 



    //Create constructor to innitilize context and data sent from MainActivity 

    public OtherOfferingAdapter(Context context,List<OtherOfferingData> data){ 
     this.context=context; 
     inflater = LayoutInflater.from(context); 
     this.data=data; 
    } 

    //Inflate the layout when viewholder created 
    @Override 
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType){ 
     View view = inflater.inflate(R.layout.container_offerings, parent, false); 
     MyHolder holder = new MyHolder(view); 
     return holder; 
    } 

    //Bind Data 
    @Override 
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position){ 
     MyHolder myHolder = (MyHolder) holder; 
     current = data.get(position); 
     myHolder.inputOfferingName.setText(current.offeringname); 
     myHolder.inputOfferingPrice.setText(current.price); 
     myHolder.inputOfferingRating.setRating(Float.parseFloat(current.rating)); 

     Picasso.with(context).load(current.imageUrl).into(myHolder.inputOfferingImage); 
     if(current.dietType.equals("1")){ 
      myHolder.inputDietType.setBackgroundResource(R.drawable.veg); 
     }else{ 
      myHolder.inputDietType.setBackgroundResource(R.drawable.nonveg); 
     } 
    } 

    @Override 
    public int getItemCount(){ 
     return data.size(); 
    } 

    class MyHolder extends RecyclerView.ViewHolder{ 

     ImageView inputOfferingImage, inputDietType; 
     TextView inputOfferingPrice, inputOfferingName; 
     RatingBar inputOfferingRating; 

     public MyHolder(View itemView){ 
      super(itemView); 

      inputOfferingImage = (ImageView) itemView.findViewById(R.id.otherimage); 
      inputDietType = (ImageView) itemView.findViewById(R.id.otherdietType); 
      inputOfferingPrice = (TextView) itemView.findViewById(R.id.otherprice); 
      inputOfferingName = (TextView) itemView.findViewById(R.id.otherofferingnanme); 
      inputOfferingRating = (RatingBar) itemView.findViewById(R.id.otherrating); 

     } 
    } 
} 

文件OtherOfferingData

public class OtherOfferingData { 
    public String imageUrl; 
    public String offeringname; 
    public String price; 
    public String dietType; 
    public String rating; 
} 

而且我打电话适配器这样

recyclerView = (RecyclerView) findViewById(R.id.recycle_view); 
         offeringAdapter = new OtherOfferingAdapter(ProductDescriptionActivity.this, data); 
         recyclerView.setAdapter(offeringAdapter); 
         recyclerView.setLayoutManager(new LinearLayoutManager(ProductDescriptionActivity.this)); 

container_offerings.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:clickable="true" 
    android:layout_marginBottom="30dp" 
    android:background="@color/lightGray" 
    android:paddingLeft="15dp" 
    android:paddingRight="15dp"> 

活动页面

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    xmlns:app="http://schemas.android.com/tools" 
    xmlns:design="http://schemas.android.com/apk/res-auto" 
    android:orientation="vertical" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

<android.support.v7.widget.RecyclerView 
         android:id="@+id/recycle_view" 
         android:layout_width="0dp" 
         android:layout_height="0dp" 
         xmlns:android="http://schemas.android.com/apk/res/android" 
         android:background="@color/lightGray" 
         android:layout_marginBottom="90dp"/> 

三江源

回答

1

中相对布局的回收视图的大小是0dp试图改变

android:layout_width="0dp" 
android:layout_height="0dp" 

既符合家长

android:layout_width="match_parent" 
android:layout_height="match_parent" 

您的回收站视图及其适配器似乎很好。但它们不会出现在屏幕上,因为它们具有0dp尺寸。

+0

是的,它工作 – parish

相关问题