6

I'm using the following dependencies in build.gradle:

compile 'com.google.firebase:firebase-database:11.4.2'
compile 'com.firebaseui:firebase-ui-database:3.1.0'
compile 'com.firebaseui:firebase-ui-auth:3.1.0' 

My goal is to populate a Listview with data from a Firebase Database. I'm following the guide on Github Firebase The following method should bind the data to a listview:

private void showMessages() {

        Query query = FirebaseDatabase.getInstance()
                .getReference("notfication/unread/" + userMailAddress.replace(".", ","))
                .orderByKey();


        FirebaseListOptions<Message> options = new FirebaseListOptions.Builder<Message>()
                .setLayout(R.layout.fbmessage_listitem)//Note: The guide doesn't mention this method, without it an exception is thrown that the layout has to be set.
                .setQuery(query, Message.class)
                .build();


        FirebaseListAdapter<Message> adapter = new FirebaseListAdapter<Message>(options) {
            @Override
            protected void populateView(View v, Message model, int position) {

                TextView tvMessage = v.findViewById(R.id.tv_message);
                tvMessage.setText(model.getDateTimeCreated());

            }
        };

        ListView readMessageList = findViewById(R.id.lvReadMessageList);
        readMessageList.setAdapter(adapter);
}

This is the layout that contains the TextViews.

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="0.6"
    android:orientation="vertical">

    <TextView
        android:id="@+id/tv_created_on"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="start"
        android:gravity="start"
        android:paddingStart="5dp"
        android:paddingEnd="0dp"
        android:textColor="@android:color/black" />

    <TextView
        android:id="@+id/tv_message"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="start"
        android:gravity="start"
        android:paddingStart="5dp"
        android:paddingEnd="0dp"
        android:textColor="@android:color/black" />

</LinearLayout>


<ImageView
    android:id="@+id/iv_message_read"
    android:layout_width="200dp"
    android:layout_height="30dp"
    android:layout_weight="0.2"
    android:contentDescription="@string/message_read"
    android:elevation="2dp"
    app:srcCompat="@drawable/android_ok_sign" />

<ImageView
    android:id="@+id/iv_delete"
    android:layout_width="200dp"
    android:layout_height="30dp"
    android:layout_weight="0.2"
    android:contentDescription="@string/abc_delete"
    android:elevation="2dp"
    android:src="@drawable/ic_delete_red_48dp" />

The model:

public class Message {

    private String message;
    private String sender;
    private String receiver;
    private long timestamp;
    private boolean isRead;
    private String userMailAddress;

    public Message() {
    }

    public Message(String message, String sender, String receiver, long timestamp, boolean isRead) {
        this.message = message;
        this.sender = sender;
        this.receiver = receiver;
        this.timestamp = timestamp;
        this.isRead = isRead;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public String getSender() {
        return sender;
    }

    public void setSender(String sender) {
        this.sender = sender;
    }

    public String getReceiver() {
        return receiver;
    }

    public void setReceiver(String receiver) {
        this.receiver = receiver;
    }

    public long getTimestamp() {
        return timestamp;
    }

    public void setTimestamp(long timestamp) {
        this.timestamp = timestamp;
    }

    public boolean isRead() {
        return isRead;
    }

    public void setRead(boolean read) {
        isRead = read;
    }

    public String getUserMailAddress() {
        return userMailAddress;
    }

    public void setUserMailAddress(String userMailAddress) {
        this.userMailAddress = userMailAddress;
    }

    public String getDateTimeCreated() {

        Calendar calendar = Calendar.getInstance(Locale.getDefault());
        calendar.setTimeInMillis(timestamp);
        String date = DateFormat.format("dd-MM-yyyy", calendar).toString();
        String time = DateFormat.format("HH:mm", calendar).toString();
        return date + "\r\n" + time;
    }
}

No matter what i try, set a breakpoint or Log a TAG, the method populateView to bind the data from the model to the textfields isn't called. I use the same layouts in the previous FirebaseUI (still in my production app) and everything is working fine there.

Does anyone know why the override method populateView isn't called?

KENdi
  • 7,576
  • 2
  • 16
  • 31
Hans
  • 523
  • 1
  • 5
  • 16

3 Answers3

10

I had the same problem, fixed it with:

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

And then

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

Works like it did before the update :-)

Jaafar Mahdi
  • 683
  • 1
  • 8
  • 15
  • Thank you. Just implemented the changes and added your suggestion. Everything works fine now! – Hans Nov 15 '17 at 12:38
6

I had the same problem. I tried the accepted answer, but the code was failing with null pointer exception in the startListening statement (adapter was null !).

After much research, I added .setLifecycleOwner(this) as in the sample code (see below code for example). It worked like a charm!

FirebaseListOptions<Message> options = new FirebaseListOptions.Builder<Message>()
    .setLayout(R.layout.fbmessage_listitem)//Note: The guide doesn't mention this method, without it an exception is thrown that the layout has to be set.
    .setQuery(query, Message.class)
    .setLifecycleOwner(this)   //Added this
    .build();
Stephen Rauch
  • 47,830
  • 31
  • 106
  • 135
Rajesh
  • 77
  • 5
3

Could it be a typo in the query? notifications instead of notification? Try putting a breakpoint in FirebaseListAdapter#onChildChanged() and see if any events come in.

Most likely, it's because you haven't called FirebaseListAdapter#startListening(). Unless you are using Android Architecture Components with FirebaseListOptions#setLifecycleOwner(...), you must manually manage your adapter's lifecycle. This means calling startListening() in onStart() and stopListening() in onStop().

PS: I would recommend checking out the RecyclerView which has better performance than ListView.

SUPERCILEX
  • 3,929
  • 4
  • 32
  • 61
  • Thanx, i think startListening() can be the solution. At this moment we don't have the time to implement the RecyclerView. I will test your solution and come back with the answer. – Hans Oct 30 '17 at 12:30
  • Did you figure it out? I have the same problem. :( – Jaafar Mahdi Nov 02 '17 at 17:46
  • I spent half a day to reach to this solution. Thank you. – Shwe Feb 25 '18 at 16:33