Reputation: 271
I am trying to create an Android app where I am using Firebase to store a list of information. I am able to store the information on the real time database without an issue, but the problem I am currently having is trying to figure out how to retrieve the list when new content is added.
When I add, change, or delete values in the list, none of the childEventListener
s are called.
FirebaseDatabase database;
DatabaseReference toDatabase;
...
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_world_play_hide);
hideAvatar = (Button) findViewById(R.id.hideAvatar);
database = FirebaseDatabase.getInstance();
toDatabase = database.getReference("jsierra");
...
hideAvatar.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
...
getCurrentLocation();
currentCoordinates = new LatLng(latitude, longitude);
data store = new data(currentCoordinates);
toDatabase.push().setValue(store);
map.addMarker(new MarkerOptions().position(currentCoordinates));
}
});
}
protected void onStart() {
apiClient.connect();
super.onStart();
DatabaseReference test = toDatabase.child("jsierra");
test.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
Toast.makeText(getBaseContext(), "Child Added", Toast.LENGTH_LONG).show();
givenData = dataSnapshot.getValue(data.class);
}
@Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
givenData = dataSnapshot.getValue(data.class);
Toast.makeText(getBaseContext(), "Child Changed", Toast.LENGTH_LONG).show();
}
@Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
givenData = dataSnapshot.getValue(data.class);
Toast.makeText(getBaseContext(), "Child Removed", Toast.LENGTH_LONG).show();
}
@Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {
givenData = dataSnapshot.getValue(data.class);
Toast.makeText(getBaseContext(), "Child Moved", Toast.LENGTH_LONG).show();
}
@Override
public void onCancelled(DatabaseError databaseError) {
Toast.makeText(getBaseContext(), "Failed", Toast.LENGTH_LONG).show();
}
});
}
Upvotes: 1
Views: 93
Reputation: 38319
Your test
reference is incorrect. Change:
DatabaseReference test = toDatabase.child("jsierra");
to:
DatabaseReference test = database.getReference("jsierra");
Upvotes: 1