简体   繁体   中英

Problem retrieve similar child element of firebase

I want to retrieve only the date and all the date element at once and store them in array in the firebase as shown below.

在此处输入图片说明

i have used the following code to retrieve but failed to that.

    public void showData(View view) {

    final ArrayList<String> date = new ArrayList<>();
    firebaseDatabase = FirebaseDatabase.getInstance().getReference().child("Date");

    firebaseDatabase.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
            date.add(dataSnapshot.getValue().toString());
            display.setText(dataSnapshot.getValue().toString());
        }

        @Override
        public void onCancelled(@NonNull DatabaseError databaseError) {

        }
    });
}

Please help to find a solution for this.

The firebase reference is wrong, you should go from the top node to the date node.

Change this:

firebaseDatabase = FirebaseDatabase.getInstance().getReference().child("Date");

into this:

firebaseDatabase = FirebaseDatabase.getInstance().getReference().child("Name");
 firebaseDatabase.addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
       for(DataSnapshot datas: dataSnapshot.getChildren()){
        date.add(datas.child("Date").getValue().toString());
           }
    }

    @Override
    public void onCancelled(@NonNull DatabaseError databaseError) {

    }
});

Since you want to get all Date then put the firebase reference at the node Name and loop inside the names to be able to retrieve all the dates.


If you want to get the date of one user example Ravi , then you do not need to loop, you can do the following:

firebaseDatabase = FirebaseDatabase.getInstance().getReference().child("Name").child("Ravi").child("Date");

firebaseDatabase.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
    date.add(dataSnapshot.getValue().toString());
       }
}

@Override
public void onCancelled(@NonNull DatabaseError databaseError) {

    }
 });

As said by @PeterHaddad in the previous answer, the sequence to access the data you have taken is not right. You may try this way, if nothing else helped until yet.

DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
rootRef.child("Names").orderByChild("Date").addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
       for(DataSnapshot ds: dataSnapshot.getChildren()){
        date.add(ds.getValue(String.class));
           }
    }

    @Override
    public void onCancelled(@NonNull DatabaseError databaseError) {

    }
});

//Also as you've told that code is not working, please tell, what exactly is not working

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM