FirebaseListAdapter不会为聊天应用推送单个项目

FirebaseListAdapter不会为聊天应用推送单个项目

本文介绍了FirebaseListAdapter不会为聊天应用推送单个项目 - Firebase-Ui 3.1的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Android上制作了一个聊天应用,它使用google firebase存储用户互相写入的消息。要向用户显示这些消息,我从数据库中读取这些消息,并将它们组织到带有ListAdapter的自定义ListView中。这工作正常,直到我更新我的依赖项,特别是firebase ui到:

I am making a chat app on Android that uses google firebase to store messages that users write to each other. To display these messages to the users I read them from the database and organize them into a custom ListView with a ListAdapter. This was working fine until I updated my dependencies, specifically firebase ui to:

com.firebaseui:firebase-ui:3.1.0

现在构建列表适配器的代码不起作用,是:

Now the code to construct the list adapter does not work, being:

adapter = new FirebaseListAdapter<ChatMessage>(FirebaseDatabase.getInstance().getReference("Lobbies").child(leaderID).child("Messages"), ChatMessage.class, R.layout.message, this) {
        @Override
        protected void populateView(View v, ChatMessage model, int position) {
            // Get references to the views of message.xml
            TextView messageText = (TextView)v.findViewById(R.id.message_text);
            TextView messageUser = (TextView)v.findViewById(R.id.message_user);
            TextView messageTime = (TextView)v.findViewById(R.id.message_time);

            // Set their text
            messageText.setText(model.getMessageText());
            messageUser.setText(model.getMessageUser());

            // Format the date before showing it
            messageTime.setText(DateFormat.format("dd-MM-yyyy (HH:mm:ss)",
                    model.getMessageTime()));
        }
    };

为了解决这个问题,我更新了代码以符合新的firebase ui要求,制作代码成为:

To fix this issue, I updated the code to conform to the new firebase ui requirements, making the code become:

FirebaseListOptions<ChatMessage> options = new FirebaseListOptions.Builder<ChatMessage>()
            .setQuery(FirebaseDatabase.getInstance().getReference("Lobbies").child(leaderID).child("Messages"), ChatMessage.class).setLayout(R.layout.message).build();

    adapter = new FirebaseListAdapter<ChatMessage>(options) {
        @Override
        protected void populateView(View v, ChatMessage model, int position) {
            // Get references to the views of message.xml
            TextView messageText = v.findViewById(R.id.message_text);
            TextView messageUser = v.findViewById(R.id.message_user);
            TextView messageTime = v.findViewById(R.id.message_time);

            // Set their text
            messageText.setText(model.getMessageText());
            messageUser.setText(model.getMessageUser());

            // Format the date before showing it
            messageTime.setText(DateFormat.format("dd-MM-yyyy (HH:mm:ss)",
                    model.getMessageTime()));
        }
    };

此代码现在编译正常,但列表视图不显示数据。是否有一种特定的正确方法来使用新的firebase ui依赖项来制作列表适配器?

This code compiles fine now, but the listview is not displaying the data. Is there a specific right way to use the new firebase ui dependency to make the list adapter?

推荐答案

让FirebaseRecyclerAdapter和FirebaseListAdapter显示活动数据



你需要使用这个:

To let the FirebaseRecyclerAdapter and FirebaseListAdapter show the data on the activity

You need to use this:

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


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

由于 FirebaseListAdapter 使用一个监听器,用于检查firebase数据库中的更改,然后在 onStart()中监听需要添加 adapter.startListening()的数据能够在列表视图中显示数据。

Since FirebaseListAdapter uses a listener to check for changes in the firebase database, then to being listening for data you need to add adapter.startListening() inside the onStart() to be able to show the data in the listview.

然后在内onStop()(当活动停止时),您可以使用 adapter.stopListening()删除适配器中的侦听器和数据。

Then inside onStop() (when activity is stopped), you can use adapter.stopListening() to remove the listener and the data in the adapter.

查看此信息以获取更多信息:

Check this for more info: Adapter LifeCycle

如果使用上述内容后,您将获得 nullpointexception 无法解析符号,您必须将 adapter 声明为全局变量请检查以下答案:出错

If after using the above, you get a nullpointexception or cannot resolve symbol, you have to declare adapter as global variable and please check the below answer: Error in startListening()

这篇关于FirebaseListAdapter不会为聊天应用推送单个项目 - Firebase-Ui 3.1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 10:27