How to get value of Child of childs from firebase in java

*爱你&永不变心* 提交于 2020-03-05 05:52:30

问题


From this I want take the value of email from mailID and subject, title from mailText.i'm able to access the value of single child but when it show null with when try get with all three.

Following code working for single child:

DatabaseReference databaseRef = database.getReference("/");
    databaseRef.addValueEventListener(new ValueEventListener() {
                 public void onDataChange(DataSnapshot dataSnapshot) {
                    ArrayList<String> email = new ArrayList();
                     for (DataSnapshot sd: dataSnapshot.getChildren()) {
                         String emailStr = sd.child("email").getValue(String.class);
                         email.add(emailStr);
                         System.out.println(email);
                         }
                     latch.countDown();
                     }

Above code gives me array contain the all email like that i want to take value of email,title and subject.


回答1:


Assuming that the new node is a direct child of your Firebase root, to achieve what you have explained in your comments, please use the following lines of code:

DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference newRef = rootRef.child("new");
ValueEventListener valueEventListener = new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        for(DataSnapshot ds : dataSnapshot.child("mailID").getChildren()) {
            String email = ds.child("email").getValue(String.class);
            String name = ds.child("name").getValue(String.class);
            Log.d("TAG", email + " / " + name);
        }
        for(DataSnapshot ds : dataSnapshot.child("mailText").getChildren()) {
            String title = ds.child("title").getValue(String.class);
            String subject = ds.child("subject").getValue(String.class);
            Log.d("TAG", title + " / " + subject);
        }
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {}
};
newRef.addListenerForSingleValueEvent(valueEventListener);

As you see, you need to attach a listener one step higher in your tree hierarchy and to loop throught the children using getChildren() method twice.




回答2:


You need to change the database structure(there is no join in firebase), to be able to query and get the required data:

new
  mailId
     email: userx@gmail.com
     name:userx
     title: test title
     Body: test body

Then you will be able to retrieve the required data for the user.

You can also use Queries to be able to get the related data, example:

DatabaseReference ref=FirebaseDatabase.getInstance().getReference().child("new");
ref.orderByChild("email").equalTo("userx@gmail.com");


来源:https://stackoverflow.com/questions/50696843/how-to-get-value-of-child-of-childs-from-firebase-in-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!