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
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. To get updates in real-time, you should change the call to addListenerForSingleValueEvent()
with addValueEventListener()
. What you are doing now, you are getting the data only once, hence that behavior. For more info, please check the official documentation:
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.