I have a RecyclerView which is supposed to display custom items using a custom adapter. I had a similar logic in a different activity in my app and it's working fine, and now I tried to do something very similar but it's not working.

I can see that deals_list (the list used to hold the items both in the adapter and the activity code) is built OK and is updated both in the adapter and the activity.

The weird thing is, no function from the Adapter is called after calling mAdapter.notifyDataSetChanged() - not getItemCount, or onCreateViewHolder and not onBindViewHolder. The list is built correctly and I update the adapter and then nothing happens!

Things I already checked/tried:

  • getItemCount isn't returning 0 but deals_list.size().

  • setLayoutManager before setting the adapter and after - both didn't work.

  • checked for common errors in the XML and found none in mine.

Please help! I'm really desperate after days of debugging and searching the web for solutions...

Relevant code from the Fragment:

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View root_view      = inflater.inflate(R.layout.fragment_live_sales, container, false);
        recyclerView        = root_view.findViewById(R.id.sales_RecyclerView);


        setupAdapter();
        loadOffersFromDb();

        return inflater.inflate(R.layout.fragment_live_sales, container, false);
    }

    private void setupAdapter() {
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        mAdapter            = new LiveSaleAdapter(deals_list);
        recyclerView.setAdapter(mAdapter);
    }

    public void loadOffersFromDb() {

                ... // creation and update of deals_list using deals_list.add and never creating a new list

                mAdapter.notifyDataSetChanged(); // after this, nothing happens. deals_list contains the items expected, at this point.
            }

            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {
            }
        });



    }

Relevant code from the Adapter:

public class LiveSaleAdapter extends android.support.v7.widget.RecyclerView.Adapter<LiveSaleAdapter.SaleViewHolder> {
    private ArrayList<Deal> mDeals;
    private Context mContext;

    public LiveSaleAdapter(ArrayList<Deal> deals) {
        mDeals = deals;
    }

    @NonNull
    @Override
    public SaleViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        mContext                     = viewGroup.getContext();
        int             layout_id    = R.layout.item_cust_offer;
        LayoutInflater  inflater     = LayoutInflater.from(mContext);
        View            view         = inflater.inflate(layout_id, viewGroup, false);

        return new SaleViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull final SaleViewHolder holder, int position) {
        Deal current_deal = mDeals.get(position);

        // updating view (a lot of calls to setText etc.)
...
    }

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

    class SaleViewHolder extends android.support.v7.widget.RecyclerView.ViewHolder {
        // Offer Details
...

        // Shop Details
        ...

        public SaleViewHolder(@NonNull View itemView) {
            super(itemView);

            // Init all views (lots of calls to findViewById)
            ...
        }
    }
}

Fragment XML:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    tools:context=".CustomerApp.LiveSales.LiveSalesFragment">

    <android.support.v7.widget.RecyclerView xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/sales_RecyclerView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layoutManager="android.support.v7.widget.LinearLayoutManager"
        tools:itemCount="3"
        tools:listitem="@layout/item_cust_offer"
        tools:orientation="vertical"
        tools:scrollbars="horizontal"
        tools:spanCount="1"
        android:nestedScrollingEnabled="false"
        android:isScrollContainer="false"
        >

    </android.support.v7.widget.RecyclerView>

</RelativeLayout>

Part of the item XML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <android.support.v7.widget.CardView xmlns:card_view="http://schemas.android.com/apk/res-auto"
        android:id="@+id/card_view"
        android:layout_width="400dp"
        android:layout_height="200dp"
        android:layout_gravity="center"
        android:layout_marginBottom="20dp"
        card_view:cardCornerRadius="10dp">

        ...


    </android.support.v7.widget.CardView>
</LinearLayout>

0 Answers