How to get the value of Async In Outside Method onDataChange ()

Sorry if my english is bad. But I hope you understand the errors in my coding.

I want to get the value from the "ambilKey ()" function to get the value for "hasilKey" in the onDataChange () external method. Please make me this code I need to getvalue "hasiKey" in the onDataChange () external method.

[MyPictureDatabaseFirebase] [1]

[MyPictureResultLog] [2]

public class IbuActivity extends AppCompatActivity { private FirebaseAuth mAuth; private String hasilKey="error"; private DatabaseReference dbAmbilKeyPengguna; protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_ibu); mAuth=FirebaseAuth.getInstance(); //Retrieve Value hasilKey is my problem ambilKey(); //Check Value Log.i("Result=",hasilKey); //hasilKey Not Working and not saved } private void ambilKey() { dbAmbilKeyPengguna=FirebaseDatabase.getInstance().getReference().child("DataPengguna").child(mAuth.getCurrentUser().getUid()).child("BisnisId"); dbAmbilKeyPengguna.addValueEventListener(new ValueEventListener() { @Override public void onDataChange(DataSnapshot dataSnapshot) { hasilKey=dataSnapshot.getValue(String.class); Log.i("ResultKeyAmbilKey=",hasilKey); // Hasil Key Value Working in this method but after variable used in outside not working } @Override public void onCancelled(DatabaseError databaseError) { Log.i("Pesan","DatabaseError"); } }); } 

}

0
source share

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


All Articles