I want to read some specific child from the parent-child, by getchild() function but this will not work properly.

enter image description here

FirebaseUser FUser = mAuth.getCurrentUser();
            String userid = FUser.getUid();

            DatabaseReference DR;
            DR = FirebaseDatabase.getInstance().getReference().child("HistoryTable").child(userid);
            DR.addValueEventListener(new ValueEventListener() {
                @Override
                public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                   // Iterable<DataSnapshot> root = dataSnapshot.getChildren();
                  //  Toast.makeText(getApplicationContext(), "ds "+dataSnapshot.getChildren(),Toast.LENGTH_LONG).show();
                    for (DataSnapshot ds: dataSnapshot.getChildren()) {
                 //       Toast.makeText(getApplicationContext(), "ds "+ds,Toast.LENGTH_LONG).show();

                        for (DataSnapshot d: ds.getChildren()) {

                            String Height = d.getKey() + d.getValue() + "\n".toString();
                            String ch =  d.child("1Height:").getValue(String.class);

                           // tv.append(Height);
                            tv.append(ch);


                        }
                    }


                }

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

                }

            });

I want to get this four child from every key.

3 Answers

0
Sandeep Malik On

you have to add your push id in reference :-

DR = FirebaseDatabase.getInstance().getReference().child("HistoryTable").child(userid).child("push id");
            DR.addValueEventListener(new ValueEventListener() {
                @Override
                public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                   // Iterable<DataSnapshot> root = dataSnapshot.getChildren();
                  //  Toast.makeText(getApplicationContext(), "ds "+dataSnapshot.getChildren(),Toast.LENGTH_LONG).show();
                    for (DataSnapshot ds: dataSnapshot.getChildren()) {
                 //       Toast.makeText(getApplicationContext(), "ds "+ds,Toast.LENGTH_LONG).show();

                        for (DataSnapshot d: ds.getChildren()) {

                            String Height = d.getKey() + d.getValue() + "\n".toString();
                            String ch =  d.child("1Height:").getValue(String.class);

                           // tv.append(Height);
                            tv.append(ch);


                        }
                    }


                }

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

                }

            });
0
Aabauser On

Try like this if you just want to read the value. if you want to read data only once use addListenerForSingleValueEvent()

*Try to read values by object stucture https://firebase.google.com/docs/database/android/read-and-write#basic_write

DR.addValueEventListener(new ValueEventListener() {
                @Override
                public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                        for (DataSnapshot d: ds.getChildren()) {
                            //below line may cause null pointer Exception
                            String Height = d.getKey() + d.getValue() + "\n".toString();
                            if(d.child("1Height:").getValue()!=null){
                            String ch =  d.child("1Height:").getValue(String.class);
                            //tv.append(Height);
                            tv.append(ch);}
                            }
                       }
                  }

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

                }

            });
0
Alex Mamo On

To get the value of your 4UserId property, simply use the following lines of code:

String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference uidRef = rootRef.child("HistoryTable").child(uid);
ValueEventListener valueEventListener = new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        for(DataSnapshot ds : dataSnapshot.getChildren()) {
            String userId = ds.child("4UserId").getValue(String.class);
            Log.d(TAG, userId);
        }
    }

    @Override
    public void onCancelled(@NonNull DatabaseError databaseError) {
        Log.d(TAG, databaseError.getMessage()); //Don't ignore errors!
    }
};
uidRef.addListenerForSingleValueEvent(valueEventListener);

The result in your logcat will be:

2Wwrjx...P2obFO83