如何在没有EventListener的情况下从Firebase检索数据? [英] How to retrieve data from Firebase without EventListener?

查看:55
本文介绍了如何在没有EventListener的情况下从Firebase检索数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在Android应用程序上使用Firebase,并且我知道如何使用EventListener从数据库中获取数据.到目前为止.

我的问题是:我有一个ReciclerView,它显示数据信息,但仅当我修改数据库时才显示.如果我关闭该应用程序然后再次打开,它什么也不会显示.然后,我进行一些修改,并将所有元素显示在容器中.

有没有什么方法可以加载数据而无需等待事件,而是要手动获取?

谢谢.

更新1(这是我的代码,可以正常工作):

databaseReference.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            listaDeLyrics.clear();
            for (DataSnapshot lyricFromFB : dataSnapshot.getChildren()){
                    LyricCard lc = lyricFromFB.getValue(LyricCard.class);
                    listaDeLyrics.add(lc);
                }
            }
        @Override
        public void onCancelled(DatabaseError databaseError) {

        }

    });

我想不使用EventListener来获取数据.

解决方案:我修复了在创建RecyclerView之前从数据库保存数据的问题.似乎没有显示任何内容,因为列表为空(但我不明白为什么它没有响应列表中的更改)

注意:addValueEvent甚至在不进行任何修改的情况下也可以首次使用.它在声明时以及事件发生时都获取信息. singleValueEvent的工作原理完全相同,不同之处在于它只执行一次,只是监听了第一个事件.

我希望这也可以对某人有所帮助.

解决方案

之所以发生这种情况,可能是因为您使用的是addValueEventListener(),它将为引用添加侦听器并监听该引用(路径)中的更改.

我建议使用addListenerForSingleValueEvent(),它将只查询一次该引用,然后从该引用中分离出来.因此,如果每次您的活动打开时都执行此操作,则每次都会有更新的值.

ref.addListenerForSingleValueEvent(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
       //parse data to recycler view adapter and call notifyDatasetChange()
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {}
});

但是您必须使用事件侦听器来读取您的Firebase数据库,没有其他方法.

I am using Firebase on my Android app and I know how to get data from the database with an EventListener. Well so far.

My problem is this: I have a ReciclerView which shows information from data but only when I modify the database. If I close the app and open again, it doesn't show anything. Then, I modify something and appears all my elements in the container.

Is there any way to load this data no waiting for an event, but getting manually?

Thank you in advance.

UPDATE 1 (This is my code which works perfectly):

databaseReference.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            listaDeLyrics.clear();
            for (DataSnapshot lyricFromFB : dataSnapshot.getChildren()){
                    LyricCard lc = lyricFromFB.getValue(LyricCard.class);
                    listaDeLyrics.add(lc);
                }
            }
        @Override
        public void onCancelled(DatabaseError databaseError) {

        }

    });

I want to get data with no using EventListener.

SOLUTION: I fix it saving data from the database before creating the RecyclerView. It seems display nothing because the list was empty (but I don't understand why it didn't response to changes on the list)

NOTE: addValueEvent works also the first time even modifying nothing. It gets information when it is declared, and then, also when an event happens. singleValueEvent works exactly the same with difference it only do once, just the first event listened.

I hope this can also help someone.

解决方案

This is happening probably because you're using addValueEventListener() which will add a listener to a reference and listen for changes in that reference(path).

What I recomend is to use addListenerForSingleValueEvent() which will query that reference only once and then dettach from it. Therefore, if you do this everytime your activity opens you'll have the updated values everytime.

ref.addListenerForSingleValueEvent(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
       //parse data to recycler view adapter and call notifyDatasetChange()
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {}
});

However you must use eventlisteners to read your firebase database, there's no other way.

这篇关于如何在没有EventListener的情况下从Firebase检索数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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