简体   繁体   English

如何每秒使用 RecyclerView 刷新 Firebase 数据库数据

[英]How can I refresh Firebase database data with RecyclerView every seconde

I create an Android app and I store data in the Firebase database and I get it, but when the user adds something new the data it does not display in the app until the user out and enter again into the app我创建了一个 Android 应用程序,并将数据存储在 Firebase 数据库中,我得到了它,但是当用户添加新内容时,它不会显示在应用程序中,直到用户退出并再次进入应用程序

I try this我试试这个

  @Override
protected void onRestart() {
    super.onRestart();
   adapter.notifyDataSetChanged();

}

*the user add the data by goes to another activity. *用户通过转到另一个活动来添加数据。 I wanna refresh the data in any moment the data add without out the app and enter again.我想在没有应用程序的情况下随时刷新数据并再次输入。 this is how I set the data to the adapter这就是我将数据设置到适配器的方式

DatabaseReference myRef = FirebaseDatabase.getInstance().getReference();
myRef.addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {

            if (dataSnapshot.exists()) {
                for (DataSnapshot ds : dataSnapshot.getChildren()) {

                    for (DataSnapshot d : ds.getChildren()) {
                        data.add(d.getValue(game.class));

                    }
                    adapter = new allAdapter(data);
                    rv.setAdapter(adapter);
                
                    allGamesAdapter.notifyDataSetChanged();

                }
            }
           
        }

        @Override
        public void onCancelled(@NonNull DatabaseError databaseError) {
           
            Toast.makeText(MainActivity.this, "error", Toast.LENGTH_SHORT).show();

        }

I wanna refresh the data at any moment the data add without out the app and enter again.我想在没有应用程序的情况下随时刷新数据并再次输入。

You are looking for the real-time feature of the Firebase Realtime Database.您正在寻找 Firebase 实时数据库的实时功能。 To get updates in real-time, you should change the call to addListenerForSingleValueEvent() with addValueEventListener() .要实时获取更新,您应该使用addValueEventListener()更改对addListenerForSingleValueEvent()的调用。 What you are doing now, you are getting the data only once, hence that behavior.你现在在做什么,你只获得一次数据,因此是这种行为。 For more info, please check the official documentation:更多信息,请查看官方文档:

暂无
暂无

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

相关问题 使用Retrofit将数据添加到数据库时,如何刷新RecyclerView? - How can I refresh a RecyclerView when I add data using Retrofit into database? 如何将 Firebase 数据库中的正确数据放入我的 RecyclerView? - How can I put correctly data from Firebase database to my RecyclerView? 添加新数据后如何刷新 RecyclerView? - How can I refresh RecyclerView after adding new data? 我无法在 recyclerView 上显示来自我的 firebase 实时数据库的数据 - I can't show data from my firebase real-time database on a recyclerView 在 Firebase 数据库中添加或更改数据时,如何停止 Recyclerview 中的内容闪烁 - How do I stop blinking of content in Recyclerview when data is added or changed in Firebase database 如何阻止从 Firebase 数据库填充并由 FirebaseRecyclerAdapter 管理的 RecyclerView 重新创建已删除的视图? - How can I stop my RecyclerView, populated from Firebase Database and managed by FirebaseRecyclerAdapter, from recreating deleted views? 删除项目后如何重新加载/刷新 RecyclerView - How can I reload/refresh the RecyclerView after deleting a item 如何从 Firebase 数据库中获取特定值(数据) - How can I fetch particular value(data) from Firebase Database 如何将 firebase 数据库的 eventlistner 的数据快照存储在字符串中 - how can I store the data snapshot of a eventlistner of firebase database in a string 如何使用不同的 Firebase 数据库名称查询数据? - How can I query data with different firebase database names?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM