[英]How to retrieve values from current signed in User Firebase,Android
我無法從我的數據快照中檢索任何值。 我嘗試了幾種方法和技術,但它給出了 null 值。
我的數據庫的圖像在上面。 請在下面查看我的代碼,嘗試記錄一個值,一切似乎都正確但不起作用。
email = view.findViewById(R.id.email);
editTextfragmentF = view.findViewById(R.id.editText_firstname);
editTextfragmentL = view.findViewById(R.id.editText_lastname);
FirebaseAuth mAuth = FirebaseAuth.getInstance();
String UserID = mAuth.getCurrentUser().getUid();
FirebaseDatabase database = FirebaseDatabase.getInstance();
database.getReference().child("Users").child(UserID).addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
if (mAuth.getCurrentUser() != null) {
email.setText(mAuth.getCurrentUser().getEmail());
//Gets current users first name
String FN = String.valueOf(dataSnapshot.child("firstName").getValue());
Log.i("First Name", FN);
/*String LN = dataSnapshot.child("lastName").getValue().toString();
Log.i("Last Name", LN);*/
/*editTextfragmentF.setText(FN);
editTextfragmentF.requestFocus();*/
/*editTextfragmentL.setText(LN);
editTextfragmentL.requestFocus();*/
}
}
結果:
你需要改變
database.getReference().child("Users").child(UserID).addChildEventListener
和
database.getReference().child("Users").child(UserID).addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
email.setText(dataSnapshot.child("email").getValue(String.class));
String FN = dataSnapshot.child("firstName").getValue(String.class);
Log.i("First Name", FN);
}
@Override
public void onCancelled(DatabaseError databaseError) {
System.out.println("The read failed: " + databaseError.getCode());
}
});
確保您已在AndroidManifest.xml
中授予互聯網訪問權限
<manifest xlmns:android...>
...
<uses-permission android:name="android.permission.INTERNET" />
<application ...
</manifest>
getValue() 接受要返回的 object 類型。 在您的情況下,您將獲得一個字符串值,因此只需將 String.class 傳遞到方法中,然后將其保存到字符串變量中。
String firstname=dataSnapshot.getValue(String.class);
Log.i("First Name", firstname);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.