【问题标题】:Android: How to retrieve data from a current user in firebase?Android:如何从firebase中的当前用户检索数据?
【发布时间】:2017-09-02 19:00:24
【问题描述】:

我正在创建一个应用程序,用户可以在其中创建帐户并登录。此外,我还有一个个人资料页面,用户可以在其中查看他的个人信息。我的问题是如何将特定或当前用户的全名、电子邮件、密码等数据检索到我的个人资料类中?

此外,我正在为我的个人资料类使用片段。

这是我的数据库结构:

这是我的代码:

_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>

我希望你能帮助我!谢谢!

【问题讨论】:

  • 您的意思是要从列表中选择特定用户并查看他/她的数据?
  • 是的。当用户登录应用程序时,他的数据将立即从 firebase 检索并显示在配置文件类中。我该怎么做?
  • 抱歉耽搁了。你弄明白了还是还需要答案?
  • @sabsab:是的,我仍然需要一个答案。我希望你能帮助我。
  • 你看到我的回答了吗?

标签: java android firebase


【解决方案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 对象应包含登录用户的数据。

【讨论】:

  • 谢谢你!但是我将如何在列表视图中显示它?
  • 改用 Recycler 视图,它是列表视图和毕业视图的现代方式和替代品。怎么做?这是一个不同的问题。这个链接对我帮助很大。它是俄语,但很容易理解。 youtube.com/watch?v=PJTs8vZKlWw&vl=en
猜你喜欢
  • 1970-01-01
  • 2019-05-03
  • 1970-01-01
  • 2017-10-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多