如何更改值并重新触发Firebase中的eventListener? [英] How to change value and re-trigger eventListener in Firebase?

查看:110
本文介绍了如何更改值并重新触发Firebase中的eventListener?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Firebase实时数据库制作公开聊天应用程序,但是一直存在这个问题。像普通的聊天应用程序,我只显示给用户几个文本,当/如果用户滚动列表更新和更多的消息显示。



我有一个监听器,它只显示(比如说10条消息)Firebase引用:

  int j = 10; ($)
myRef.child(Chat)。limitToLast(j).addChildEventListener(new com.google.firebase.database.ChildEventListener(){
@Override
public void onChildAdded(com.google (Map< String,Object>)dataSnapshot.getValue();

Map< String,Object> datamsnapshot,String s){
// Map < String,Object> msg =(Map< String,Object>)dataSnapshot.getValue();


String text = msg.get(Text)。toString();
values.add(text);


@Override
public void onChildChanged(com.google.firebase.database.DataSnapshot dataSnapshot,String s){

}

@Override
public void onChildRemoved(com.google.firebase.database.DataSnapshot dataSnapshot){

}

@Override
public void onChildMoved(com.google.firebase.database.Dat aSnapshot dataSnapshot,String s){

}
$ b $ @覆盖
public void onCancelled(DatabaseError databaseError){

}
});

现在我有了一个滚动监听器来改变 j j + 10 ,但是ChildEvenListener没有被触发,新的消息也没有添加到列表视图中。

我也尝试过使用查询,但在更新值后又遇到同样的问题,应如何更新适配器?



没有添加新的子项,因此childevent侦听器没有被触发,但是我想要改变

< pre $ limitToLast(j)

并重新填充列表。
任何帮助,将不胜感激。 解决方案

Firebase查询是不可变的。如果您更改传递给 limitToLast()的值,它将成为新的查询。所以你必须附加一个新的侦听器到新的查询,然后重新填充该侦听器的项目列表。

如果您跟踪列表中每个现有项目的键( dataSnapshot.getKey()),则可以通过检查是否优化列表的更新每个来自新侦听器的项目已经出现在列表中。


I am making a public chat app using Firebase Real-time Database, but stuck on this problem. Like normal chat apps, I am displaying only few texts to the user and when/if the user scrolls the List is updated and more messages are displayed.

I have a listener to my Firebase reference, which is only displaying (say 10 messages):

int j = 10;
 myRef.child("Chat").limitToLast(j).addChildEventListener(new com.google.firebase.database.ChildEventListener() {
        @Override
        public void onChildAdded(com.google.firebase.database.DataSnapshot dataSnapshot, String s) {
         //   Map<String,Object> datemsg = (Map<String,Object>)dataSnapshot.getValue();

             Map<String,Object> msg = (Map<String,Object>)dataSnapshot.getValue();


                        String text = msg.get("Text").toString();
                        values.add(text);


        @Override
        public void onChildChanged(com.google.firebase.database.DataSnapshot dataSnapshot, String s) {

        }

        @Override
        public void onChildRemoved(com.google.firebase.database.DataSnapshot dataSnapshot) {

        }

        @Override
        public void onChildMoved(com.google.firebase.database.DataSnapshot dataSnapshot, String s) {

        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });

Now I have a scroll Listener that changes the value for j to j+10 but then the ChildEvenListener is not triggered and the new messages are not added to the listview.

I also tried using queries, but again having the same problem, after updating the value, how should I update the adapter?

No new child is added, thus the childevent listener is not triggered, but I want to change the size of

limitToLast(j) 

and re-populate the list. Any help would be appreciated.

解决方案

Firebase queries are immutable. If you change the value you pass into limitToLast(), it becomes a new query.

So you must attach a new listener to the new query, and then repopulate the list with the items from that listener.

If you keep track of the key (dataSnapshot.getKey()) for each existing item in your list, you can optimize the updating of the list by checking if each item from the new listener is already present in the list.

这篇关于如何更改值并重新触发Firebase中的eventListener?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆