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
基础教程推荐
- 如何对 HashSet 进行排序? 2022-01-01
- 如何在不安装整个 WTP 包的情况下将 Tomcat 8 添加到 Eclipse Kepler 2022-01-01
- 首次使用 Hadoop,MapReduce Job 不运行 Reduce Phase 2022-01-01
- Java 中保存最后 N 个元素的大小受限队列 2022-01-01
- 如何使用 Stream 在集合中拆分奇数和偶数以及两者的总和 2022-01-01
- 在螺旋中写一个字符串 2022-01-01
- 如何使用 Eclipse 检查调试符号状态? 2022-01-01
- 如何强制对超级方法进行多态调用? 2022-01-01
- 由于对所需库 rt.jar 的限制,对类的访问限制? 2022-01-01
- Spring Boot Freemarker从2.2.0升级失败 2022-01-01