parish
parish

Reputation: 856

Recycle View adapter not loading the items

I am new to android I have created recycler view with its adapter but it is not working. check the following code.

I tried to debug this activity but there is no error. and also in OtherOfferingAdapter program don't execute further after getItemCount().

File 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);

        }
    }
}

File OtherOfferingData

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

And I am calling adapter like this

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

Activity Page

<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"/>

ThankYou

Upvotes: 1

Views: 918

Answers (1)

Kevin LE GOFF
Kevin LE GOFF

Reputation: 770

The size of your recycler view in the relative layout is 0dp try to change

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

both to match parent

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

Your recycler view and its adapter seems to be fine. But they don't appear on screen as they have 0dp as dimension.

Upvotes: 2

Related Questions