Hans
Hans

Reputation: 521

Android Using FirebaseUI with FirebaseListOptions to populate a ListView doesn't call populateView

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?

Upvotes: 6

Views: 4243

Answers (3)

Rajesh
Rajesh

Reputation: 87

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();

Upvotes: 7

Jaafar Mahdi
Jaafar Mahdi

Reputation: 713

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 :-)

Upvotes: 10

SUPERCILEX
SUPERCILEX

Reputation: 4007

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.

Upvotes: 3

Related Questions