handle empty data view in recyclerview

Question!

How to handle empty data View in a RecyclerView ,I have tried so many ways from internet but none seems to work. I am also using realm database so I don't know if this is the right way to check if it is empty or not.

this is my Fragment xml where the RecyclerView is located

<?xml version="1.0" encoding="utf-8"?>

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/tv_no_data"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@android:color/holo_green_dark"
        android:gravity="center"
        android:text="emptty"
        android:textAppearance="?android:textAppearanceMedium"
        android:textColor="@android:color/white"
        android:visibility="invisible" />

    <android.support.v7.widget.RecyclerView
        android:id="@+id/rv_favorite"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/colorPrimaryDark">

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

</FrameLayout>

and this is my fragment class

public class FavouriteFragment extends Fragment {
RecyclerView mRecyclerView;
FavouriteAdapter adapter;
Realm mRealm;
int positions;
TextView emptyText;
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View view=inflater.inflate(R.layout.favourite_fragment,container,false);
    mRealm=Realm.getDefaultInstance();
    RealmQuery<news> quotesRealmQuery = mRealm.where(News.class).equalTo("favourite",true);
    RealmResults<News> mResults = newsRealmQuery.findAll();

    emptyText= (TextView) view.findViewById(R.id.tv_no_data);

    //adapter=new FavouriteAdapter(getActivity(),mResults,mRealm);
    mRecyclerView= (RecyclerView) view.findViewById(R.id.rv_favorite);
    mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));

    if (!mResults.isEmpty()) {
        //if data is available, don't show the empty text
        emptyText.setVisibility(View.INVISIBLE);
        adapter=new FavouriteAdapter(getActivity(),mResults,mRealm,pos,single);
        mRecyclerView.setAdapter(adapter);
        mRealm.addChangeListener(new RealmChangeListener<Realm>() {
            @Override
            public void onChange(Realm element) {
                adapter.notifyItemRemoved(positions);
            }
        });

    } else
        emptyText.setVisibility(View.VISIBLE);




    return view;

}


Answers

Your RecyclerView is not transparent so you should remember to hide it when is empty:

  if (!mResults.isEmpty()) {
            //if data is available, don't show the empty text

            emptyText.setVisibility(View.INVISIBLE);
            mRecyclerView.setVisibility(View.VISIBLE);

        } else { 
            emptyText.setVisibility(View.VISIBLE);
            mRecyclerView.setVisibility(View.GONE);
        }
By : wdeb


There are 2 ways to achieve this:

  1. When user moves from activity B to activity C, close the socket connection and save the "timestamp" of the last message in preferences. Now when the user is in activity C, your server can push the updates lets say "SYNC" through FCM (or GCM) which tells your app that there is a new update on the server. On receiving this notification from server, call the server pass the "timestamp" stored from the preferences to server. Now, server's job is to return the list of messages after the passed "timestamp". Store all the message in local database. Once the user comes back to the activity B, load the new messages from the local database.

  2. Keep the socket connection maintained when moving to another activity. Reconnect to the socket connection on opening of activity C.

You can check our open source android chat sdk code for reference: https://github.com/AppLozic/Applozic-Android-SDK



The best solution would be create an IntentService which will keep your socket connection alive and keep fetching the messages in the device and when you go back to activity B, in onResume you can fetch all the latest messages form the local storage and update the list adapter.

By : Anirudh


This video can help you solving your question :)
By: admin