0

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?

    //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();
        }
    }); }

enter image description here

I figured out the issue. I have added the corrected code below.

             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);

1 Answers1

0

Maybe its because you are calling twice child("Bio"), first in the DatabaseReference and then in the DataSnapshot. Try to delete it from one of them.