Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Current user to string

I faced a problem with showing data. Before I used current users UID as a key but now I’m using the custom key and I don’t know how to call this key as a string in my code.

My code this part ( child(user.getUid() )

enter image description here

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

FirebaseUser userUID;
userUID = FirebaseAuth.getInstance().getCurrentUser();

        final DatabaseReference NameRef = database.getReference().child("No server").child(userUID.getUid()).child("name");
        final DatabaseReference LastNameRef = database.getReference().child("No server").child(userUID.getUid()).child("lastName");
        final DatabaseReference EmailRef = database.getReference().child("No server").child(userUID.getUid()).child("email");

What changed

enter image description here

I want make user a string "dan ned".

Edit:

 FirebaseDatabase database = FirebaseDatabase.getInstance();

        final DatabaseReference NameRef = database.getReference().child("No server").child(user.getUid()).child("name");
        final DatabaseReference LastNameRef = database.getReference().child("No server").child(user.getUid()).child("lastName");
        final DatabaseReference EmailRef = database.getReference().child("No server").child(user.getUid()).child("email");

      NameRef.addValueEventListener(new ValueEventListener() {
          @Override
          public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
              if (dataSnapshot.exists()){
                  final String strName = dataSnapshot.getValue(String.class);
                  textName.setText(strName);
              }
          }

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

          }
      });

        LastNameRef.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                if (dataSnapshot.exists()){
                    final String strLastName = dataSnapshot.getValue(String.class);
                    textLastName.setText(strLastName);
                }
            }

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

            }
        });

        EmailRef.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                if (dataSnapshot.exists()){
                    final String strEmail = dataSnapshot.getValue(String.class);
                    textEmail.setText(strEmail);
                }
            }

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

            }
        });

>Solution :

There is no need to attach three listeners in order to read those three values. You can use only one, as you can see in the following lines of code:

String name = FirebaseAuth.getInstance().getCurrentUser().getDisplayName();
DatabaseReference db = FirebaseDatabase.getInstance().getReference();
DatabaseReference nameRef = db.child("No server").child(name);
nameRef.get().addOnCompleteListener(new OnCompleteListener<DataSnapshot>() {
    @Override
    public void onComplete(@NonNull Task<DataSnapshot> task) {
        if (task.isSuccessful()) {
            DataSnapshot snapshot = task.getResult();
            String key = snapshot.getKey();
            Log.d("TAG", key); //It will print "dan ned"
            String email = snapshot.child("email").getValue(String.class);
            Log.d("TAG", email); //It will print "DanilNed@gmail.com"
            String lastName = snapshot.child("lastName").getValue(String.class);
            Log.d("TAG", lastName); //It will print "Ned"
            String name = snapshot.child("name").getValue(String.class);
            Log.d("TAG", name); //It will print "Dan"
        } else {
            Log.d("TAG", task.getException().getMessage()); //Never ignore potential errors!
        }
    }
});
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading