How to return a DataSnapshot value as a result of a method?

I do not have much experience with Java. I'm not sure this question is stupid, but I need to get the username from the Firebase database in real time and return that name as a result of this method. So, I figured out how to get this value, but I don’t understand how to return it as a result of this method. What is the best way to do this?

private String getUserName(String uid) {
    databaseReference.child(String.format("users/%s/name", uid))
            .addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            // How to return this value?
            dataSnapshot.getValue(String.class);
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {}
    });
}
+20
source share
4 answers

-API. , . , onDataChange() null. , onDataChange() . , .

Firebase Realtime Database , -API , . , ( ), , . , , onDataChange(), . , onDataChange() onDataChange() .

, , , .

private String getUserName(String uid) {
    Log.d("TAG", "Before attaching the listener!");
    databaseReference.child(String.format("users/%s/name", uid)).addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            // How to return this value?
            dataSnapshot.getValue(String.class);
            Log.d("TAG", "Inside onDataChange() method!");
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {}
    });
    Log.d("TAG", "After attaching the listener!");
}

, :

!

!

onDataChange()!

, , , , null .

, "" asynchronous behavior, . , , -API.

, . , " , ", " . , ". , , , onDataChange() , :

databaseReference.child(String.format("users/%s/name", uid)).addListenerForSingleValueEvent(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        // How to return this value?
        if(dataSnapshot != null) {
            System.out.println(dataSnapshot.getValue(String.class));
        }
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {}
});

, . , , Firebase . interface:

public interface MyCallback {
    void onCallback(String value);
}

, . :

public void readData(MyCallback myCallback) {
    databaseReference.child(String.format("users/%s/name", uid)).addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            String value = dataSnapshot.getValue(String.class);
            myCallback.onCallback(value);
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {}
    });
}

readData() MyCallback , , :

readData(new MyCallback() {
    @Override
    public void onCallback(String value) {
        Log.d("TAG", value);
    }
});

onDataChange(). .

+56

, onDataChange, TextView textview.setVisiblity(Gone) textview.setVisiblity(Gone) - , -

textview.setText(dataSnapshot.getValue(String.class))

textview.getText().toString()

.

+3

, , . , "" ( ) , , , . , , :

  • ( )
  • , .

, . 4 4b - :

4. Edit: , , yourNameVariable ( , yourNameVariable )

4b. Edit: , onClickListener.


.

// 1. Create a variable at the top of your class
private String yourNameVariable;

// 2. Retrieve your value (which you have done mostly correctly)
private void getUserName(String uid) {
    databaseReference.child(String.format("users/%s/name", uid))
            .addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            // 3. Set the public variable in your class equal to value retrieved
            yourNameVariable = dataSnapshot.getValue(String.class);
            // 4a. EDIT: now that your fetch succeeded, you can trigger whatever else you need to run in your class that uses `yourNameVariable`, and you can be sure `yourNameVariable` is not null.
            sayHiToMe();
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {}
    });
}

// (part of step 4a)
public void sayHiToMe() {
  Log.d(TAG, "hi there, " + yourNameVariable);
}

// 4b. use the variable in a function triggered by the onClickListener of a button.
public void helloButtonWasPressed() {
  if (yourNameVariable != null) {
    Log.d(TAG, "hi there, " + yourNameVariable);
  }
}

yourNameVariable , , .


. , , yourNameVariable , onDataChange , .

+2

This works, you can use the values ​​from datasnapshot outside onDataChange by passing the value as a parameter to another function, see my answer below, it works, and I use it everywhere in my application.

// Get Your Value
private void getValue() {

    fbDbRefRoot.child("fbValue").addListenerForSingleValueEvent(new ValueEventListener() {

        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {

            String yourValue = (String) dataSnapshot.getValue();
            useValue(yourValue);

        }

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

        }
    });

}

// Use Your Value
private void useValue(String yourValue) {

    Log.d(TAG, "countryNameCode: " + yourValue);

}

The main difference between my answer and Rbar's answer is that I pass the value as a parameter to the function and then use it.

0
source

Source: https://habr.com/ru/post/1690901/


All Articles