Reputation: 453
I am building my first project in android using Firebase
.
I have build a Firebase's database
which is having main node as post, media as its child node, media node can contain multiple values for a particular post or can have one value inside it.
So i want to fetch media child node values with particular post node.
Could you please help me out.
Any help will deeply appreciated.
Please check the screenshots below.
Upvotes: 1
Views: 3428
Reputation:
First you need to read docs to know how to retrieve data from firebase.
and your data will be like this ...
final Firebase ref = new Firebase("URL/posts");
ref.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot userSnapshot : dataSnapshot.getChildren()) {
//u can handle your variables name in GetData class
GetData posts = userSnapshot.getValue(GetData.class);
In another way..
DatabaseReference databaseReference = FirebaseDatabase.getInstance().getReference().child("posts");
String user_id = firebaseAuth.getCurrentUser().getUid();
databaseReference.child(user_id).child("media").getValue("user_id").getValue(etc);
You might save last line as String
to reuse it anywhere.
UPDATE:
And to Fitch data You have to use Firebase adapter, example
Upvotes: 1
Reputation: 3737
If just want to fetch the value once, you can do like this.
FirebaseDatabase.getInstance().getReference("posts").child("your_post_item_id").child("media").addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot children : dataSnapshot.getChildren()) {
YourMediaModel mediaItem = children.getValue(YourMediaModel.class);
//add you mediaItem to list that you provided
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
Upvotes: 0
Reputation: 3964
Firebase ref = new Firebase(YOUR_FIREBASE_URL);
String postNode ="KddShng........";
ref.child("posts/"+postNode).child("media").addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
Log.e("!_@@_Key : >",dataSnapshot.getKey()); // you will get key of Media node
}
@Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
}
@Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
}
@Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {
}
@Override
public void onCancelled(FirebaseError firebaseError) {
}
});
Upvotes: 1
Reputation: 737
Try something like this
FirebaseDatabase.getInstance().getReference("posts").child(UID).child("media");
Where UID is the key of the particular post node.
PS: this returns something like a promise, for which you need to attach a listener for. (because this is real time)
Upvotes: 0