Ad

How To Get The Name Of Parent A+ For This Code?

I am unable to access this parent node using that code.

public void onComplete(@NonNull Task<AuthResult> task) {
    if (!task.isSuccessful()) {
        Toast.makeText(LoginActivity.this,"Login error", Toast.LENGTH_SHORT).show();
    } else {
        String user = FirebaseAuth.getInstance().getCurrentUser().getUid();
        DatabaseReference mRef = FirebaseDatabase.getInstance().getReference();
        final Query userQuery = mRef.orderByChild(user);
        userQuery.addChildEventListener(new ChildEventListener() {
            @Override
            public void onChildAdded(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
                map.clear();
                String myParentNode = dataSnapshot.getKey();
                for (DataSnapshot child: dataSnapshot.getChildren()) {
                    String key = child.getKey().toString();
                    String value = child.getValue().toString();
                    map.put(key, value);
                }
                Intent intent = new Intent(LoginActivity.this, UserMapActivity.class);
                intent.putExtra("bloodType",myParentNode);
                startActivity(intent);
            }
        }
    }  
}     

i want to get the highlighted parent from the underline child in every session

Ad

Answer

To get A+, please use the following lines of code:

String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference usersRef = rootRef.child("Users");
Query query = usersRef.orderByChild(uid).equalTo(true);
ValueEventListener valueEventListener = new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        for(DataSnapshot ds : dataSnapshot.getChildren()) {
            Log.d(TAG, ds.getKey());
            //Do what you need to do with your key
        }
    }

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

The output in the logatcat will be:

A+
Ad
source: stackoverflow.com
Ad