Reputation: 1
Stop retrieve data from Firebase after its loaded.
This is my code:
databaseReference.child("SHARE").addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot ds : dataSnapshot.getChildren()) {
information spacecraft = ds.getValue(information.class);
spacecrafts.add(spacecraft);
}
adapter adapterView= new adapter(MainActivity.this, spacecrafts);
gridView.setAdapter(adapterView);
}
}
Upvotes: 0
Views: 1246
Reputation: 1221
Use addListenerForSingleValueEvent()
databaseReference.child("SHARE").addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot ds : dataSnapshot.getChildren()) {
information spacecraft = ds.getValue(information.class);
spacecrafts.add(spacecraft);
}
adapter adapterView= new adapter(MainActivity.this, spacecrafts);
gridView.setAdapter(adapterView);
}
have a look on this answer https://stackoverflow.com/a/41579337/5868103
Upvotes: 2
Reputation: 692
Use addListenerForSingleValueEvent instead of addValueEventListener if you want to read the whole data once.
Or if you want to get update from onDataChange, you can remove the listner,
ValueEventListener eventListener = databaseReference.child("SHARE").addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot ds : dataSnapshot.getChildren()) {
information spacecraft = ds.getValue(information.class);
spacecrafts.add(spacecraft);
}
adapter adapterView= new adapter(MainActivity.this, spacecrafts);
gridView.setAdapter(adapterView);
}
To remove the listener,
databaseReference.child("SHARE").removeEventListener(eventListener);
Upvotes: 1