1

I've followed a few different docs to pull together my first Firebase Recycler View:

The two main ones:

Here and here.

I'm using a Message object (included further down) with the Firebase Recycler View. For some reason the data is not displaying. Judging from my log outputs, nothing in the adaptor is being called. Here is the activity code that sits in my onCreate():

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_message_details);


        RecyclerView mRecyclerView;
        mRecyclerView = (RecyclerView) findViewById(R.id.recyclerView);
        mRecyclerView.setHasFixedSize(true);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));

        Query query = myMessagesRef.child(RoomID)
                .orderByKey()
                .limitToLast(50);

        FirebaseRecyclerOptions<Message> options =
                    new FirebaseRecyclerOptions.Builder<Message>()
                            .setQuery(query, Message.class)
                            .build();

        FirebaseRecyclerAdapter adapter = new FirebaseRecyclerAdapter<Message, ChatHolder>(options) {


            @Override
            public ChatHolder onCreateViewHolder(ViewGroup parent, int viewType) {
                // Create a new instance of the ViewHolder, in this case we are using a custom
                // layout called R.layout.message for each item
                final View view = LayoutInflater.from(parent.getContext())
                        .inflate(R.layout.recycler_item, parent, false);

                return new ChatHolder(view);
            }

            @Override
            protected void onBindViewHolder(ChatHolder holder, int position, Message model) {
                //super.onBindViewHolder(holder, position);
                Log.w(TAG, "Some Info on messages 2 " + model.MessageText);
                holder.bindChat(model);

            }



        };
        mRecyclerView.setAdapter(adapter);

    }

Here is my ViewHolder:

static class ChatHolder extends RecyclerView.ViewHolder{
    TextView mtext;
    //Context mContext;


    public ChatHolder(View v) {
        super(v);
        mtext = (TextView) v.findViewById(com.example.administrationuser.piclo.R.id.textView13);
    }

    public void bindChat(Message mess){
        mtext.setText(mess.MessageText);
    }


}

Here is my Message object that I am passing to both Firebase and the adaptor (the data that I input syncs to Firebase with no issue, and my query to firebase gets the data in correct format):

public static class Message {

    public String UserID;
    public String UserName;
    public String MessageText;

    public Message() {}  // Needed for Firebase

    public Message(String UserID, String UserName, String MessageText) {
        this.UserID = UserID;
        this.UserName = UserName;
        this.MessageText = MessageText;
    }
}

Here is my Activity Layout XML (with name: activity_message_details.xml): (note the recycler view near the bottom):

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.administrationuser.piclo.MessageDetails">

    <LinearLayout
        android:layout_width="395dp"
        android:layout_height="643dp"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        android:orientation="vertical"
        tools:layout_editor_absoluteY="8dp"
        tools:layout_editor_absoluteX="8dp">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="150dp"
            app:layout_behavior="@string/appbar_scrolling_view_behavior">

            <EditText
                android:id="@+id/editText3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:ems="10"
                android:inputType="textPersonName"
                android:text="Name"
                app:layout_behavior="@string/appbar_scrolling_view_behavior" />

            <Button
                android:id="@+id/button9"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentRight="true"
                android:text="Button"
                app:layout_behavior="@string/appbar_scrolling_view_behavior"
                android:onClick="onClick"/>
        </RelativeLayout>

        <android.support.v7.widget.RecyclerView
            android:id="@+id/recyclerView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:drawSelectorOnTop="false"
            />



    </LinearLayout>




</android.support.constraint.ConstraintLayout>

Finally, here is my View layout XML (with name: recycler_item.xml):

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

    <TextView
        android:id="@+id/textView13"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="TextView" />

</FrameLayout >
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Christopher Mills
  • 711
  • 10
  • 28
  • For future visitors, you can take a look **[here](https://stackoverflow.com/questions/49277797/how-to-display-data-from-firestore-in-a-recyclerview-with-android/49277842)**, where I have explained step by step how to display data from Firestore into a `RecyclerView` using Android. – Alex Mamo Mar 14 '18 at 13:08

2 Answers2

3

There were two things that needed fixing.

1) Adding in the adapter.startListening(); (as mentioned by Elvin No Matter). In my case I added this just below the mRecyclerView.setAdapter(adapter); near the end of my onCreate.

2) Enclosing my View Layout XML in a <android.support.v7.widget.LinearLayoutCompat>. See my new View Holder, with some other irrelevant changes to the buttons:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.LinearLayoutCompat
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
 >

    <android.support.constraint.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginStart="16dp"
        android:layout_marginEnd="16dp"
        android:layout_marginTop="8dp"
        android:layout_marginBottom="8dp">

        <TextView
            android:id="@+id/textView13"
            android:layout_width="285dp"
            android:layout_height="24dp"
            android:text="TextView"
            android:layout_alignParentTop="true"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            tools:layout_editor_absoluteX="-6dp" />

        <TextView
            android:id="@+id/textView14"
            android:layout_width="123dp"
            android:layout_height="24dp"
            android:layout_alignParentTop="true"
            android:layout_marginRight="8dp"
            android:text="TextView"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintRight_toRightOf="@+id/textView13"
            app:layout_constraintTop_toTopOf="parent"
            tools:layout_editor_absoluteX="154dp" />

    </android.support.constraint.ConstraintLayout>

</android.support.v7.widget.LinearLayoutCompat>
Christopher Mills
  • 711
  • 10
  • 28
1

according to readme

The FirestoreRecyclerAdapter uses a snapshot listener to monitor changes to the Firestore query. To begin listening for data, call the startListening() method. You may want to call this in your onStart() method. Make sure you have finished any authentication necessary to read the data before calling startListening() or your query will fail.

@Override
protected void onStart() {
    super.onStart();
    adapter.startListening();
}

and

@Override
protected void onStop() {
    super.onStop();
    adapter.stopListening();
}