1

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.

enter image description here

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.

Peter Haddad
  • 78,874
  • 25
  • 140
  • 134
Ranjit Vamadevan
  • 514
  • 5
  • 21
  • The path you are giving is not correct, you are missing 2 nodes. It should be FirebaseDatabase.getInstance().getReference().child("Name").child(actual_name).child("Date"); – Shruti Sep 07 '18 at 07:18
  • the problem is not solved with this. – Ranjit Vamadevan Sep 07 '18 at 09:09
  • One way is like you have to set your reference until the "Name" node any then you have to traverse manually for the "Date". – Mayur Patel Sep 07 '18 at 11:15
  • @RanjitVamadevan do mark the answer as correct and vote it up, this helps the stack overflow readers and I'd appreciate that too. Cheers! :) – PradyumanDixit Nov 16 '18 at 04:33

2 Answers2

1

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) {

    }
 });
Peter Haddad
  • 78,874
  • 25
  • 140
  • 134
0

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
PradyumanDixit
  • 2,372
  • 2
  • 12
  • 20