我正在創建一個應用程序,用戶可以創建一個帳戶,也可以登錄。此外,我有一個用戶可以查看其個人信息的個人資料頁面。我的問題是如何從特定或當前用戶檢索全名,電子郵件,密碼等數據到我的個人檔案類中?Android:如何從Firebase中的當前用戶檢索數據?
另外,我爲我的個人檔案類使用了一個片段。
這裏是我的數據庫結構:
這裏是我的代碼:
_8_ViewEventMember_Profile.java
public class _8_ViewEventMember_Profile extends Fragment {
private FirebaseAuth auth;
TextView name, email, password, bday, country, mobileno;
View myView;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
myView = inflater.inflate(R.layout.activity__8__view_event_member_profile, container, false);
((AppCompatActivity) getActivity()).getSupportActionBar().setTitle("Profile");
name = (TextView) getActivity().findViewById(R.id.textName);
email = (TextView) getActivity().findViewById(R.id.textEmail);
password = (TextView) getActivity().findViewById(R.id.textPassword);
bday = (TextView) getActivity().findViewById(R.id.textCountry);
country = (TextView) getActivity().findViewById(R.id.textCountry);
mobileno = (TextView) getActivity().findViewById(R.id.textMobileNumber);
auth = FirebaseAuth.getInstance();
FirebaseAuth.AuthStateListener authListener = new FirebaseAuth.AuthStateListener() {
@Override
public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
FirebaseUser firebaseUser = firebaseAuth.getCurrentUser();
if(firebaseUser != null){
String userName = firebaseUser.getDisplayName();
String userEmail = firebaseUser.getEmail();
FirebaseDatabase db = FirebaseDatabase.getInstance();
String key = db.getReference("accounts").push().getKey();
Map<String, Object> childUpdates = new HashMap<>();
childUpdates.put("fullname", userName);
name.setText(userName);
email.setText(userEmail);
}
}
};
return myView;
}
}
activity__8__view_event_member_profile.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="20dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="20dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Name:"
android:textColor="@color/colorBlack"
android:textSize="20sp"/>
<TextView
android:id="@+id/textName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Name"
android:textColor="@color/colorBlack"
android:textSize="20sp"
android:layout_marginLeft="55dp"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="20dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Email:"
android:textColor="@color/colorBlack"
android:textSize="20sp"/>
<TextView
android:id="@+id/textEmail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Email"
android:textColor="@color/colorBlack"
android:textSize="20sp"
android:layout_marginLeft="60dp"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="20dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Password:"
android:textColor="@color/colorBlack"
android:textSize="20sp"/>
<TextView
android:id="@+id/textPassword"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Password"
android:textColor="@color/colorBlack"
android:textSize="20sp"
android:layout_marginLeft="23dp"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="20dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Birthday:"
android:textColor="@color/colorBlack"
android:textSize="20sp"/>
<TextView
android:id="@+id/textBday"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Birthday"
android:textColor="@color/colorBlack"
android:textSize="20sp"
android:layout_marginLeft="40dp"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="20dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Country:"
android:textColor="@color/colorBlack"
android:textSize="20sp"/>
<TextView
android:id="@+id/textCountry"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Country"
android:textColor="@color/colorBlack"
android:textSize="20sp"
android:layout_marginLeft="45dp"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="20dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Mobile No.:"
android:textColor="@color/colorBlack"
android:textSize="20sp"/>
<TextView
android:id="@+id/textMobileNumber"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Mobile Number"
android:textColor="@color/colorBlack"
android:textSize="20sp"
android:layout_marginLeft="20dp"/>
</LinearLayout>
</LinearLayout>
我希望你能幫助我!謝謝!
你的意思是你想從列表中選擇特定的用戶並查看他/她的數據? – sabsab
是的。當用戶在應用程序中登錄時,他的數據將立即從firebase中檢索並顯示在配置文件類中。我將如何做到這一點? –
抱歉,延遲。你弄明白了,還是需要回答? – sabsab