我正在创建一个用户可以创建帐户并且也可以登录的应用程序。此外,我还有一个用户可以查看其个人信息的个人资料页面。我的问题是如何从特定或当前用户检索全名,电子邮件,密码等数据到我的个人资料类?
此外,我正在为我的个人资料类使用片段。
这是我的数据库结构:
这是我的代码:
_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>
我希望你能帮助我!谢谢!
答案 0 :(得分:1)
在进行开发之前,您必须structure you data以用户指定的方式对数据进行反规范化,展平和复制。在决定最终结构之前,您应该知道检索数据的方式和时间。如果您计划好数据库结构,那么您将简化编程过程,并节省大量时间。
我的解决方案是将用户树内用户的密钥复制为用户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
对象应包含登录用户的数据。