Java Android:如何从firebase中的当前用户检索数据?
我正在创建一个应用程序,用户可以在其中创建帐户,也可以登录。我还有一个个人资料页面,用户可以在其中查看他的个人信息。我的问题是,我如何从特定用户或当前用户检索全名、电子邮件、密码等数据到我的profile类中 此外,我正在为我的profile类使用一个片段 以下是我的数据库结构: 这是我的密码: _8_ViewEventMember_Profile.javaJava Android:如何从firebase中的当前用户检索数据?,java,android,firebase,Java,Android,Firebase,我正在创建一个应用程序,用户可以在其中创建帐户,也可以登录。我还有一个个人资料页面,用户可以在其中查看他的个人信息。我的问题是,我如何从特定用户或当前用户检索全名、电子邮件、密码等数据到我的profile类中 此外,我正在为我的profile类使用一个片段 以下是我的数据库结构: 这是我的密码: _8_ViewEventMember_Profile.java public class _8_ViewEventMember_Profile extends Fragment { private F
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;
}
}
活动\uuuu 8\uuuu视图\u事件\u成员\u 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>
我希望你能帮助我!谢谢大家! 在进行开发之前,您必须以用户指定的方式对数据进行非规范化、扁平化和复制。在决定最终结构之前,您应该知道如何以及何时检索数据。如果您对数据库结构进行了很好的规划,您将简化编程过程,并将节省大量时间 我的解决方案是将用户树中的用户密钥复制为用户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
// 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());
}
});
然后将侦听器附加到该特定用户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
// 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对象应包含登录用户的数据。您的意思是要从列表中选择特定用户并查看其数据吗?是。当用户登录应用程序时,其数据将立即从firebase检索并显示在profile类中。我该怎么做?抱歉耽搁了。你找到答案了吗?还是你还需要答案?@sab:是的,我还需要答案。我希望你能帮助我。你看到我的答案了吗?谢谢你!但是我如何在列表视图中显示它呢?改用Recycler视图,它是列表视图和渐变视图的现代方式和替代品。怎么做?这是另一个问题。这个链接对我帮助很大。它是俄语,但很容易理解。