title: Display user data in alert dialog from Firebase Database
body: <p>Im trying to get a user's information to be displayed in an alert dialog box when their profile is clicked on. I am able to get the dialog box to be displayed when clicked but the dialog box is empty. The users data is being stored in Firebase Real-Time Database.Iv posted the code Im using and added comments to the code. Im guessing the issue is with the Database Reference or the DataSnapshot?</p>
  
  <pre><code>    //Variables
      usersDb = FirebaseDatabase.getInstance().getReference().child("Users");
      mAuth = FirebaseAuth.getInstance();
      currentUId = mAuth.getCurrentUser().getUid();
  
      // OnItemClickListener
      flingContainer.setOnItemClickListener(new SwipeFlingAdapterView.OnItemClickListener() {
          @Override
          public void onItemClicked(int itemPosition, Object dataObject) {
  
                 // users profile card 
              cards obj = (cards) dataObject;
              final String userId = obj.getUserId();
  
              final AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
  
              DatabaseReference userbio = usersDb.child(userId).child("Bio");
              userbio.addValueEventListener(new ValueEventListener() {
                  @Override
                  public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                      if (dataSnapshot.exists()){
                          if (dataSnapshot.child(userId).child("Bio").getValue() != null){
                          userBio = dataSnapshot.child("Bio").getValue().toString();
                      }
                  }
                  }
  
                  @Override
                  public void onCancelled(@NonNull DatabaseError databaseError) {
  
                  }
              });
  
              builder.setTitle("Bio");
              builder.setMessage(userBio);
              // Set click listener for alert dialog buttons
              DialogInterface.OnClickListener dialogClickListener = new DialogInterface.OnClickListener() {
                  @Override
                  public void onClick(DialogInterface dialog, int which) {
                      switch(which){
                          case DialogInterface.BUTTON_POSITIVE:
                              // User clicked the Yes button
                              break;
  
                          case DialogInterface.BUTTON_NEGATIVE:
                              // User clicked the No button
                              break;
                      }
                  }
              };
              // Set the alert dialog yes button click listener
              builder.setPositiveButton("Yes", dialogClickListener);
              // Set the alert dialog no button click listener
              builder.setNegativeButton("No",dialogClickListener);
              AlertDialog dialog = builder.create();
              // Display the alert dialog on interface
              dialog.show();
          }
      }); }
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/U8Vb1.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/U8Vb1.png" alt="enter image description here"></a></p>
  
  <p>I figured out the issue. I have added the corrected code below.</p>
  
  <pre><code>             DatabaseReference databaseReference = usersDb.child(userId);
               databaseReference.addValueEventListener(new ValueEventListener() 
                    {
                   @Override
                   public void onDataChange(@NonNull DataSnapshot dataSnapshot) 
                     {
                       if (dataSnapshot.exists()) {
                           if (dataSnapshot.child("Bio").getValue() != null) {
                               userBio = dataSnapshot.child("Bio").getValue().toString();
                           }
                       }}
                   @Override
                   public void onCancelled(@NonNull DatabaseError databaseError) {
  
                   }
               });
              final AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
  
              builder.setTitle("Bio");
              builder.setMessage(userBio);
  </code></pre>