如何在外部方法onDataChange()中获得值异步

时间:2017-05-24 08:17:45

标签: android firebase firebase-realtime-database firebase-authentication

对不起,如果我的英语不好。 但我希望你能理解我编码中的错误。

我想从函数中检索值" ambilKey()"获得" hasilKey"的价值在外部方法onDataChange()中。请把我需要的代码告诉我#34; hasiKey"在外部方法onDataChange()。

[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 个答案:

没有答案