简体   繁体   English

如何再次从firebase数据库中获取数据?

[英]How to ask data from firebase database again?

I am having an app with a Swipe-To-Refresh layout. 我正在使用具有“ 刷卡到刷新”布局的应用程序。

When you scroll up in a Swipe-To-Refresh layout, data refreshes from the internet. 在“滑动到刷新”布局中向上滚动时,数据将从Internet刷新。 (for those who don't know) (对于那些不知道的人)

My data source is Firebase. 我的数据源是Firebase。

In my app if the user is not connected, then it shows a network error message, then after the internet is turned on it doesn't get the data. 在我的应用程序中,如果用户未连接,则显示网络错误消息,然后在打开互联网后,它不会获取数据。

How do you I get data again from my database. 你如何从我的数据库中再次获取数据。

Here is the code: 这是代码:

protected void onCreate(Bundle savedInstanceState) {

    ...

    mFirebaseDatabase = FirebaseDatabase.getInstance();
    // my database

    mNewsDatabaseReference = LoginActivity.mFirebaseDatabase.getReference()
    .child("data").child("news");
    // my database reference

    // my ChildEventListener
    mNewsChildEventListener = new ChildEventListener() {
        @Override
        public void onChildAdded(DataSnapshot dataSnapshot, String s) {

            // my data
            News news = dataSnapshot.getValue(News.class);

            // add the news to the top of my ArrayAdapter
            myAdapter.insert(news, 0);
        }
        @Override
        public void onChildChanged(DataSnapshot dataSnapshot, String s) {}
        @Override
        public void onChildRemoved(DataSnapshot dataSnapshot) {}
        @Override
        public void onChildMoved(DataSnapshot dataSnapshot, String s) {}
        @Override
        public void onCancelled(DatabaseError databaseError) {}
    };

    // my OnRefreshListener
    mySwipeRefreshLayout.setOnRefreshListener(
            new SwipeRefreshLayout.OnRefreshListener() {
                @Override
                public void onRefresh() {

                    // what Should I do in here to request the data again?!

                    // I know that I should empty my adapter first then request the data
                    myAdapter.clear();

                }
            }
    );
}

When using firebase realtime-database, there is no need to do the request again. 使用firebase实时数据库时,无需再次执行请求。 Once the data changes, it will automatically fetch the data for you and one of your listeners is going to be called. 一旦数据发生变化,它将自动为您获取数据,并且将调用其中一个侦听器。

Your onChildChanged() event will be fired any time a news child node is modified, including any modifications to descendants of the child node. 每次修改news子节点时都会触发onChildChanged()事件,包括对子节点后代的任何修改。

To enable Offline Capabilities don't forget to use setPersistenceEnabled(true) 要启用脱机功能,请不要忘记使用setPersistenceEnabled(true)

Check out the documentation for more info 查看文档以获取更多信息

The answer above is most helpful, but for the question how to request data again?! 上面的答案是最有帮助的,但对于如何再次请求数据的问题?!

You simply need to attach the listeners one more time. 您只需再次附加侦听器即可。

But remember to detach the old listeners first so that data doesn't appear twice in the adapter. 但请记住首先分离旧的侦听器,以便数据不会在适配器中出现两次。

Here is the code: 这是代码:

    mNewsSwipeRefresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
        @Override
        public void onRefresh() {
            myAdapter.clear();
            mNewsDatabaseReference.removeEventListener(mNewsChildEventListener);
            mNewsDatabaseReference.addChildEventListener(mNewsChildEventListener);
        }
    });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM