1

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:- enter image description here

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.

Peter Haddad
  • 78,874
  • 25
  • 140
  • 134
enemy123
  • 43
  • 9
  • what is not working? – Peter Haddad Apr 14 '18 at 09:20
  • I am able to retrieve a complete post using on child event listener and recyclerview, problem I have is I can't link the user and post together. I have stored user's id when he makes the post especially for that purpose so that I can retrieve his information later, basically I am showing user's info along with post but I can't achieve that. What I want is that when someone clicks post in recyclerview it shows user info based on that user's id,how can I retrieve user's info based on user Id in that post? – enemy123 Apr 14 '18 at 13:58

1 Answers1

1

To retrieve user info based on his id:

DatabaseReference ref=FirebaseDatabase.getInstance().getReference();
postRef= FirebaseDatabase.getInstance().getReference().child("Posts").child("Posts");
postRef.orderByChild("tittle").equalTo(post_here).addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
            for(DataSnapshot ds: dataSnapshot.getChildren()){
                String userid=ds.child("userid").getValue().toString();
               ref.child("Users").child("Users").orderByKey().equalTo(userid).addValueEventListener(new ValueEventListener() {
                @Override
                public void onDataChange(DataSnapshot dataSnapshot) {
                    for(DataSnapshot datas: dataSnapshot.getChildren()){
                  String fullname=dataSnapshot.child("address").getValue().toString();
                //other data
                 }
              }
                  @Override
                 public void onCancelled(DatabaseError databaseError) {

                      }
                   });    
              }
          }
      }

    @Override
    public void onCancelled(DatabaseError databaseError) {
    }
  });
}

After the user clicks on a post, you can retrieve the data of that post with the user's id and then you can retrieve, the user's info by doing orderByKey().equalTo(userid)

Peter Haddad
  • 78,874
  • 25
  • 140
  • 134
  • Thanks for help gonna try it, what is post_here and why compare it with tittle? – enemy123 Apr 14 '18 at 14:59
  • because you said you have a recyclerview with posts, so to be able to retrieve the id related to that post do orderByChild("tittle").equalTo(post_here), I assumed that the recyclerview contains tittle of the posts that when clicking you can then compare it with the one in the database – Peter Haddad Apr 14 '18 at 15:40
  • One more question,how can I pass this user detail to my single report view activity,which I display in onclick event of recyclerview item. I can't use intents because these will make the derived variables out of scope right? Because onclick listener is defined somewhere else in class and I retrieve these values in another method so they are out of scope. Any suggestions? thanks. – enemy123 Apr 14 '18 at 16:45
  • not sure, but using https://stackoverflow.com/questions/24471109/recyclerview-onclick retrieve the values and just use intent to pass them to another activity onclick – Peter Haddad Apr 14 '18 at 16:49
  • Is there any problem? – Peter Haddad Apr 15 '18 at 18:10
  • Logic is okay and it might wok for someone else but it didn't work for me I didn't get userid from this method. Instead I got it another way,by storing alerts data in alerts model class I had and from that I got it in the list. I am novice to stackoverflow should I still flag this answer to accepted or should I update it with what worked for me? Because I think this should work in some situation elsewhere. – enemy123 Apr 15 '18 at 20:20