我正在创建一个应用程序,用户可以在其中创建帐户并登录。此外,我还有一个个人资料页面,用户可以在其中查看他的个人信息。我的问题是如何将特定或当前用户的全名、电子邮件、密码等数据检索到我的个人资料类中?
此外,我正在为我的配置文件类使用一个 fragment 。
这是我的数据库结构:
这是我的代码:
_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>
希望你能帮助我!谢谢你!
最佳答案
在进行开发之前,您必须 structure you data以用户规定的方式对数据进行反规范化、扁平化和复制。在决定最终结构之前,您应该知道如何以及何时检索该数据。如果您对数据库结构进行了良好的规划,您将简化您的编程过程并节省大量时间。
我的解决方案是将用户树中的用户 key 复制为用户 ID,如下所示:
acounts:
---- -KSOMEKEYVALUE
--------brithday:*****
--------fullname:*****
--------id: KSOMEKEYVALUE
然后,当登录过程成功时,您可以根据特定用户的唯一 ID 检索其数据,如下所示:
DatabaseReference ref = database.getReference("accounts");
login=ref.child(userID); //here user will sign in using his name so it is
//based on userID, directory will change dynamically
然后将监听器附加到该特定用户 ID,如下所示:
// Attach a listener to read the data at our posts reference
login.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
FirebaseUser firebaseUser = dataSnapshot.getValue(Post.class);
}
@Override
public void onCancelled(DatabaseError databaseError) {
System.out.println("The read failed: " + databaseError.getCode());
}
});
firebaseUser
对象应包含登录用户的数据。
关于java - Android:如何从 Firebase 中的当前用户检索数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46017199/