我想在我的數據庫獲取單個值,但我發現它很難通過一個字符串。這裏是我的代碼:如何從onDataChange返回值?
FirebaseUser user = firebaseAuth.getCurrentUser();
databaseReference = FirebaseDatabase.getInstance().getReference("USERS").child(user.getUid()).child("name");
databaseReference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
String name = (String) dataSnapshot.getValue();
editTextName.setText(name);
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
String name2 = editTextName.getText().toString().trim();
我怎樣才能訪問字符串名稱以外呢? 我編輯我的代碼,通過它說,
invoke virtual method 'android.text.Editable android.widget.EditText.getText()' on a null object reference
Firebase異步加載數據。最好接受並修改你的代碼來處理它。見https://stackoverflow.com/questions/33203379/setting-singleton-property-value-in-firebase-listener –