Mehvish Faisal Mehvish Faisal - 6 days ago 5
Android Question

RecyclerView not displaying any data

In my Application I m having a

RecyclerView
in one of my Fragments in which I am getting response from a server using
Reterofit
. Every thing is fine but My
RecylerView
is not displaying any data returned from my service. Even the

int count = offerRecyclerAdapter.getItemCount();


of my adapter is returning the correct amount of data count returned from server.

Inside Fragment:

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
offersRecyclerView = (RecyclerView) view.findViewById(R.id.offer_recycler);

MotorCityArabiaGlobal global = (MotorCityArabiaGlobal) getActivity().getApplication();
ApiInterface apiService=global.getClient().create(ApiInterface.class);
Call<OfferResponse> call = apiService.getOffers(4);
call.enqueue(new Callback<OfferResponse>() {
@Override
public void onResponse(Call<OfferResponse> call, Response<OfferResponse> response) {
OfferResponse offers = response.body();
if(offers!=null){

List<Offer> result = response.body().getResult();
offerRecyclerAdapter = new OffersAdapter(getActivity(),result);

offersRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
offersRecyclerView.setItemAnimator(new DefaultItemAnimator());
offersRecyclerView.addItemDecoration(new DividerItemDecoration(getActivity(), LinearLayoutManager.VERTICAL));
offersRecyclerView.addOnItemTouchListener(new RecyclerView.OnItemTouchListener() {
@Override
public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent e) {
return false;
}

@Override
public void onTouchEvent(RecyclerView rv, MotionEvent e) {

}

@Override
public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {

}
});

int count = offerRecyclerAdapter.getItemCount();


offerRecyclerAdapter.notifyDataSetChanged();


}
}

@Override
public void onFailure(Call<OfferResponse> call, Throwable t) {

}
});
}


Adapter Class:

public class OffersAdapter extends RecyclerView.Adapter<OffersAdapter.MyViewHolder> {
private List<Offer> offersList= Collections.emptyList();
private LayoutInflater inflater;
Context ctx;

public OffersAdapter(Context ctx,List<Offer> offersList) {
this.ctx = ctx;
this.inflater = LayoutInflater.from(ctx);
this.offersList = offersList;
}

@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
// View view = LayoutInflater.from(parent.getContext())
// .inflate(R.layout.offer_list_layout, parent, false);
View view = inflater.inflate(R.layout.offer_list_layout,parent,false);
return new MyViewHolder(view);
}

@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
Offer offer = offersList.get(position);
holder.title.setText(offer.getOffer_title());
holder.price.setText(offer.getPrice());
holder.offerCount.setText(offer.getOffer_count());

}

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

public class MyViewHolder extends RecyclerView.ViewHolder {
public TextView title, offerCount, price;
public ImageView image;

public MyViewHolder(View view) {
super(view);
title = (TextView) view.findViewById(R.id.title);
offerCount = (TextView) view.findViewById(R.id.offer_count);
price = (TextView) view.findViewById(R.id.price);
image = (ImageView)view.findViewById(R.id.car_img);
}
}
}


home_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent"

>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="@+id/header_wrapper"
>
<!-- Row1-->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"

>
<!-- Column1 row 1-->
<LinearLayout
android:layout_width="140dp"
android:layout_height="125dp"
android:layout_weight="1"
android:background="@drawable/new_car_selector"
android:clickable="true"

android:layout_marginTop="5dp"
android:layout_marginRight="5dp"
android:layout_marginBottom="5dp"
></LinearLayout>

<!-- Column2 row 1-->
<LinearLayout
android:layout_width="140dp"
android:layout_height="125dp"
android:layout_weight="1"
android:background="@drawable/find_car_selector"
android:clickable="true"
android:layout_marginLeft="5dp"
android:layout_marginTop="5dp"

android:layout_marginBottom="5dp"
></LinearLayout>


</LinearLayout>

<!-- Row2-->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="2"

>
<!-- Column1 row 2-->


<!-- Column2 row 2-->
<LinearLayout
android:layout_width="140dp"
android:layout_height="125dp"
android:layout_weight="1"
android:background="@drawable/sell_car_selector"
android:clickable="true"

android:layout_marginTop="5dp"
android:layout_marginRight="5dp"
android:layout_marginBottom="10dp"
></LinearLayout>

<LinearLayout
android:layout_width="140dp"
android:layout_height="125dp"
android:layout_weight="1"
android:background="@drawable/compare_car_selector"
android:clickable="true"
android:layout_marginLeft="5dp"
android:layout_marginTop="5dp"

android:layout_marginBottom="10dp"
></LinearLayout>


</LinearLayout>

</LinearLayout>
<!-- Header Wrapper ends-->

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/header_wrapper"
android:id="@+id/offers_recycler_wrapper"
android:orientation="vertical"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="CAR OFFERS"
android:textColor="@color/colorAccentDark"
android:textStyle="bold"
android:textSize="16sp"
android:layout_marginLeft="5dp"

/>

<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/offer_recycler"
></android.support.v7.widget.RecyclerView>

</LinearLayout>
</RelativeLayout>


offer_list.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent"
android:background="?android:attr/selectableItemBackground"
android:orientation="vertical"
android:paddingTop="6dp"
android:paddingBottom="5dp"
>
<ImageView
android:layout_width="80dp"
android:layout_height="60dp"
android:src="@drawable/sell_car_select"
android:id="@+id/car_img"

/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/meta_data"
android:orientation="vertical"
android:layout_toEndOf="@id/car_img"
android:layout_marginLeft="5dp"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TITLE"
android:textSize="16dp"
android:textColor="#000"
android:textStyle="bold"
android:id="@+id/title"

/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="3dp"
>

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/price"
android:text="13,000"
android:textStyle="bold"
android:textColor="@color/colorAccentDark"
android:textSize="16dp"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/curr"
android:textColor="#BDBDBD"
android:text=" SAUDI RAYAL"
android:textSize="12dp"

/>


</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/offer_count_wrapper"
android:orientation="horizontal"
android:layout_marginTop="3dp"
>
<ImageView
android:layout_width="12dp"
android:layout_height="12dp"
android:src="@drawable/ic_offer_orange"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="12dp"
android:text=" 3 Offers"
android:id="@+id/offer_count"
/>
</LinearLayout>


</LinearLayout>

</RelativeLayout>

Answer

You did not set the adapter to the RecyclerView. Use setAdapter method. See the following code,

List<Offer> results = new ArrayList<Offer>();

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);

    offersRecyclerView = (RecyclerView) view.findViewById(R.id.offer_recycler);
    offersRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
    offersRecyclerView.setItemAnimator(new DefaultItemAnimator());
    offersRecyclerView.addItemDecoration(new DividerItemDecoration(getActivity(), LinearLayoutManager.VERTICAL));

    offerRecyclerAdapter = new OffersAdapter(getActivity(), results)
    offersRecyclerView.setAdapter(offerRecyclerAdapter);

    offersRecyclerView.addOnItemTouchListener(new RecyclerView.OnItemTouchListener() {
        @Override
        public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent e) {
            return false;
        }

        @Override
        public void onTouchEvent(RecyclerView rv, MotionEvent e) {

        }

        @Override
        public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {

        }
    });


    MotorCityArabiaGlobal global = (MotorCityArabiaGlobal) getActivity().getApplication();
    ApiInterface apiService=global.getClient().create(ApiInterface.class);
    Call<OfferResponse> call = apiService.getOffers(4);
    call.enqueue(new Callback<OfferResponse>() {
        @Override
        public void onResponse(Call<OfferResponse> call, Response<OfferResponse> response) {
            OfferResponse offers = response.body();
            if(offers != null){

                List<Offer> result = response.body().getResult();
                results.clear();
                results.addAll(result);
                offerRecyclerAdapter.notifyDataSetChanged();
            }
        }

        @Override
        public void onFailure(Call<OfferResponse> call, Throwable t) {

        }
    });
}
Comments