[英]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.