MohanRaj MohanRaj - 1 month ago 5
Android Question

Empty Recycler view is not getting updated with new data

I intialize my recycler view adapter with an empty arraylist initially.
Then later once I fetch data from API I try to update the adapter by calling

notifyDataSetChanged()
but none of call back methods in my adapter is getting fired

void initRecyclerView()
{
myList = new ArrayList<>();
myRecyclerAdapter = new myRecyclerAdapter(getActivity(), myList, "");
myRecyclerView.setAdapter(myRecyclerAdapter);
linearLayoutManager = new LinearLayoutManager(getActivity());
myRecyclerView.setHasFixedSize(true);
myRecyclerView.setLayoutManager(linearLayoutManager);
}


After getting data from API I just update the adapter as follow :

void updateRecyclerView(ArrayList<Data> newList)
{
if (myList.isEmpty())
linearLayoutManager.removeAllViews();
myList.clear();
myList.addAll(newList);
myRecyclerAdapter.notifyDataSetChanged();
}

Answer

I made a costliest mistake and ponder around something else instead of looking it at different aspect.

I had SwipeRefreshLayout that wraps RecyclerView.

I should've just use setRefreshing(false) once data downloaded through API call. Instead I used setVisibility(VIEW.GONE) on mymSwipeRefreshLayout`.

This visibility change of SwipeRefreshLayout impacted RecyclerView within.

I hope none will do this mistake. Perhaps someone might come across this tiniest mistake so I Mark my answer as accepted answer.