How to get the specified data child added by push() method in firebase realtime Database?
Hi I have following data childs in firebase realtime database:-
I am using child.push()
method to save data so that it generates a random key whenever a Posts child is registered. Each post also has a userid of the user, who posted it, which is sort of a primary key as well.
Following is my code to write new post child:-
private void writeNewPost(String userId, String tittle, String address, String locationLat, String locationLong, String date, String extrass, String postId) {
Alerts alerts = new Alerts(userId, tittle, address, date, locationLat, locationLong, extrass, postId);
String key = mDatabaseUsers.push().getKey();
// String key = mDatabase.child("posts").push().getKey();
mDatabaseUsers.child("Posts").child(key).setValue(alerts, new DatabaseReference.CompletionListener() {
public void onComplete(DatabaseError error, DatabaseReference ref) {
Log.d("ssd", "onComplete: " + error);
}
});
}
Now I want to retrieve a specific post but I can't understand how, for example I have a RecyclerView
where I show all the posts. When a user clicks on a specific post that post should be retrieved along with data from firebase but I can't understand how. I have used addValueEventListener
like this :-
mDatabase.child("Posts").addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
final String key = mDatabase.push().getKey();
// String name = (String)
dataSnapshot.child("name").getValue();
// String mobile = (String) dataSnapshot.child("phoneNumber").getValue();
// String additionalIn = (String) dataSnapshot.child("extras").getValue();
// String address = (String) dataSnapshot.child("address").getValue();
// String profile_pic = (String) dataSnapshot.child("address").getValue();
String post_uid = (String) dataSnapshot.child("userid").getValue();
String post_tittle = (String) dataSnapshot.child("tittle").getValue();
// if (mAuth.getCurrentUser().getUid().equals(post_uid)){
//
// deleteBtn.setVisibility(View.VISIBLE);
// }
}
@Override
public void onCancelled(DatabaseError databaseError) {
Log.d("OMG", "onCancelled: " + databaseError);
}
});
}
}
but I can't understand how to retrieve a specific child in an onClick()
or any other similar callback method.