1

I am developing an android application and now I am stuck in retrieving data from particular nodes like I want to retrieve only one value from each nodes. The database structure shows below.

Database Structure

How can I retrieve the second unique id that created by Firebase database?

KENdi
  • 7,576
  • 2
  • 16
  • 31
Alex
  • 77
  • 1
  • 9
  • Do you have reference of AppointmentStatus parent node ? – Umair Khalid Sep 02 '18 at 17:33
  • I do not know how to get AppointmentStatus parent node but i can get the parent key which is 3dp8ich...ect – Alex Sep 02 '18 at 17:37
  • Alex you need to have parent id -LLPF0pDH.. otherwise there is workaround in which you will have to get all child of 3dp8ich.. in HashMap and then will have to convert it into array then iterate through all children and one by one get AppointmentStatus's value – Umair Khalid Sep 02 '18 at 17:42
  • @Alex You want to get the value of `appointmentStuts` only from the first node `-LLPF0pDH...` or from all nodes? Please responde with @. – Alex Mamo Sep 03 '18 at 09:33
  • @AlexMamo i want to get only the appointmentstuts under -LLPF0pdh node – Alex Sep 03 '18 at 11:02
  • @Alex Do you have this value `3dp8ich` stored in a variable? If yes, what is the name of the variable that holds that value? – Alex Mamo Sep 03 '18 at 11:09
  • @AlexMamo i was stored as current user id but the problem this currentUser id store in all nested children with the same value 3dp8ich. As a result, when I queried this value the application fetched all current user ids from the nested children – Alex Sep 03 '18 at 11:31
  • mDatabase = FirebaseDatabase.getInstance().getReference().child("Appointments").child(PostKey); the postkey is the father key whcih is 3dp8ich...etc , want to get the seond key with its values @AlexMamo – Alex Sep 03 '18 at 11:34

2 Answers2

1

First create a POJO class to get the values you want, it should be writen the same way as you have them in Firebase.

public class AppointmentsPojo {

    private String appointmentStuts;

    public AppointmentsPojo(){

    }


    public String getAppointmentStuts() {
        return appointmentStuts;
    }

    public void setAppointmentStuts(String appointmentStuts) {
        this.appointmentStuts = appointmentStuts;
    }

}

Then just loop inside Appointments to get each appointmentStuts

mDatabase.child("Appointments").addListenerForSingleValueEvent(new ValueEventListener() {
  @Override
  public void onDataChange(DataSnapshot dataSnapshot) {
    //Looping inside Appointments to get each appointmentsStuts
    for(DataSnapshot snapshot: dataSnapshot.getChildren()){
    AppointmentsPojo ap = snapshot.getValue(AppointmentsPojo.class);
    //Getting each appointmentStuts
     String appointmentStuts = ap.getAppointmentStuts();
     //To get each father of those appointments 
     String key = snapshot.getKey();

      Log.e("Data: " , "" + appointmentStuts );

       }

  }

  @Override
  public void onCancelled(DatabaseError databaseError) {
    System.out.println("The read failed: " + databaseError.getCode());
  }
});

Where mDatabase is

DatabaseReference mDatabase;

mDatabase = FirebaseDatabase.getInstance().getReference();
Gastón Saillén
  • 12,319
  • 5
  • 67
  • 77
  • It worked but the list all available values under the parent id, I want to display only the appointmentStuts under its key, like now when I click the button the application display Available value twice instead of only once – Alex Sep 02 '18 at 18:44
  • yes, because its fetching all the appointmentStuts from all the keys, you can add .orderByKey(keyid).addListener..... – Gastón Saillén Sep 02 '18 at 18:49
  • The keyid is basically is created into OnDataChange methods, so how I can pass it to the mDatabase.OrderbyId. i do not know how to pass the keyid. Thanks in advance – Alex Sep 02 '18 at 19:10
  • you can store it in a global variable and then pass it to this other query – Gastón Saillén Sep 02 '18 at 19:12
0

To get the value of appointmentStuts only from the first object, please use the following code:

DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
Query query = rootRef.child("Appointments").child(PostKey).limitToFirst(1);
ValueEventListener valueEventListener = new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        for(DataSnapshot ds : dataSnapshot.getChildren()) {
            String appointmentStuts = ds.child("appointmentStuts").getValue(String.class);
            Log.d(TAG, appointmentStuts);
        }
    }

    @Override
    public void onCancelled(@NonNull DatabaseError databaseError) {
        Log.d(TAG, databaseError.getMessage());
    }
};
query.addListenerForSingleValueEvent(valueEventListener);
Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
  • that is cool, what is about if I want to get the value from the second or third i mean whenever i want it – Alex Sep 03 '18 at 12:31
  • In that case, you should use a query on a specific property like this: `Query query = rootRef.child("Appointments").child(PostKey).orderByChild("specificProperty").equalsTo("specificValue");`. If you don't have a specific property yet, you shoul consider adding for example, the `uid` and in such case, the query should look like this: `Query query = rootRef.child("Appointments").child(PostKey).orderByChild("uid").equalsTo(uid);`. – Alex Mamo Sep 03 '18 at 12:38
  • Please also take a look at my answer from this **[post](https://stackoverflow.com/questions/51787784/how-to-get-specific-pushedid-in-firebase/51788244)** where I have explained also how you can get that pushed id that is missing from your referece. So having that id you can get any object that you want. – Alex Mamo Sep 03 '18 at 12:40
  • It is very helpful only one thing please, I have created now Appointmentkey and store it as one of the values through using push().getKey() but now I have to comper this value with what. Thanks in advance – Alex Sep 03 '18 at 13:15
  • It's good that you have strored the value of the pushed id. In this case, you can get that particular value and use it directly in your reference like this: `DatabaseReference idRef = rootRef.child("Appointments").child(PostKey).child(pushedId)`. That's it! Is it ok now? – Alex Mamo Sep 03 '18 at 13:18
  • i did like this but does not work , DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference(); final Query query = rootRef.child("Appointments").child(PostKey).child("appointmentKey"); – Alex Sep 03 '18 at 13:29
  • You need to use `.child(appointmentKey)` without quotation marks because `appointmentKey` is a variable and you want to use its value and not the string `"appointmentKey"`, right? Try to hardcode that pueshd id first and see if it works. – Alex Mamo Sep 03 '18 at 13:31
  • I did but now the problem my push id key is generated before store it in the database once I retrieve the key it retrieved the key that just generated which is not sorted yet in the database. Thanks again and again – Alex Sep 03 '18 at 13:38
  • You were asking hoe to retrieve the data, not to sort. If you want some kind of sort, you should make your own attempt given and ask another question if something else comes up so me and other users can help you. – Alex Mamo Sep 03 '18 at 13:43
  • Did my answer helped you? – Alex Mamo Sep 05 '18 at 06:32
  • Can someone tell me what is PostKey in this answer? If its a variable, its nowhere to be found in above code. If it's a keyword, Android Studio saying, can't resolve symbol. – Mahadev May 16 '19 at 15:34
  • @Mahadev Is the key of a particular post. – Alex Mamo May 20 '19 at 08:11
  • @AlexMamo Thank you. I found the answer though. https://stackoverflow.com/questions/56172005/retrieve-node-id-from-firebase-database-on-cardview-click/56173353#56173353 – Mahadev May 20 '19 at 09:00