FirebaseListAdapter not pushing individual items for chat app - Firebase-Ui 3.1(FirebaseListAdapter 不推送聊天应用程序的单个项目 - Firebase-Ui 3.1)
问题描述
我正在 Android 上制作一个聊天应用程序,它使用 google firebase 来存储用户相互写的消息.为了向用户显示这些消息,我从数据库中读取它们并将它们组织到带有 ListAdapter 的自定义 ListView 中.在我更新我的依赖项,特别是 firebase ui 之前,这一切正常:
com.firebaseui:firebase-ui:3.1.0
现在构造列表适配器的代码不起作用,是:
adapter = new FirebaseListAdapter(FirebaseDatabase.getInstance().getReference("Lobbies").child(leaderID).child("Messages"), ChatMessage.class, R.layout.message, this) {@覆盖protected void populateView(View v, ChatMessage model, int position) {//获取对 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);//设置他们的文本messageText.setText(model.getMessageText());messageUser.setText(model.getMessageUser());//在显示之前格式化日期messageTime.setText(DateFormat.format("dd-MM-yyyy (HH:mm:ss)",模型.getMessageTime()));}};
为了解决这个问题,我更新了代码以符合新的 firebase ui 要求,使代码变为:
FirebaseListOptions<ChatMessage>options = new FirebaseListOptions.Builder().setQuery(FirebaseDatabase.getInstance().getReference("Lobbies").child(leaderID).child("Messages"), ChatMessage.class).setLayout(R.layout.message).build();适配器 = 新 FirebaseListAdapter(选项){@覆盖protected void populateView(View v, ChatMessage model, int position) {//获取对 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);//设置他们的文本messageText.setText(model.getMessageText());messageUser.setText(model.getMessageUser());//在显示之前格式化日期messageTime.setText(DateFormat.format("dd-MM-yyyy (HH:mm:ss)",模型.getMessageTime()));}};
这段代码现在编译得很好,但是列表视图没有显示数据.是否有特定的正确方法来使用新的 firebase ui 依赖项来制作列表适配器?
让FirebaseRecyclerAdapter和FirebaseListAdapter显示activity上的数据
你需要使用这个:
@Override受保护的无效 onStart() {super.onStart();适配器.startListening();}@覆盖受保护的无效 onStop() {超级.onStop();适配器.stopListening();}
由于 FirebaseListAdapter
使用侦听器来检查 firebase 数据库中的更改,因此要侦听您需要在 中添加
能够在列表视图中显示数据.adapter.startListening()
的数据>onStart()
然后在onStop()
内部(当activity停止时),你可以使用adapter.stopListening()
来移除监听器和adapter中的数据.p>
查看更多信息:适配器生命周期
注意:
如果使用上述方法后,你得到一个 nullpointexception
或 cannot resolve symbol
,你必须将 adapter
声明为全局变量,请检查以下答案:startListening() 中的错误
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()));
}
};
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()));
}
};
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?
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();
}
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.
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
Note:
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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:FirebaseListAdapter 不推送聊天应用程序的单个项目 - Firebase-Ui 3.1
- 未找到/usr/local/lib 中的库 2022-01-01
- java.lang.IllegalStateException:Bean 名称“类别"的 BindingResult 和普通目标对象都不能用作请求属性 2022-01-01
- 获取数字的最后一位 2022-01-01
- 在 Java 中,如何将 String 转换为 char 或将 char 转换 2022-01-01
- 如何指定 CORS 的响应标头? 2022-01-01
- 如何使 JFrame 背景和 JPanel 透明且仅显示图像 2022-01-01
- 将 Java Swing 桌面应用程序国际化的最佳实践是什么? 2022-01-01
- Eclipse 的最佳 XML 编辑器 2022-01-01
- 转换 ldap 日期 2022-01-01
- GC_FOR_ALLOC 是否更“严重"?在调查内存使用情况时? 2022-01-01