如何根据用户的Google帐号显示他们的数据 – Firebase

huangapple go评论74阅读模式
英文:

How to show each users' data based on their Google account - Firebase

问题

我目前正在使用Android Studio构建一个待办事项应用程序。我在使用Firebase数据库时遇到了一个问题。我的目标是,每个用户根据他们的Google登录拥有一个唯一的标识符,以便只能查看自己的数据。目前,在我的应用程序中,每个人都可以看到任何人的任务。在Firebase上,我正在使用实时数据库,通过一个名为“Users”的子级和另一个名为Task +随机数字的子级来指定它。此外,我已经在我的应用程序启动时添加了Google登录按钮。

当前数据库:
插入图像描述

我希望层次结构的示例:
插入图像描述

英文:

I'm currently building a to-do app on Android Studio. I'm facing an issue with my Firebase database. What I'm trying to do is, each user to have a unique id based on their google sign in, in order to see only their own data. Currently, on my app, everyone can see anyone's' tasks. On Firebase I'm using the Realtime Database, specifying it by one child of "Users" and another one of Task + a random number. Also, I've already added the google sign in button on the launch of my app.

Current Database:
enter image description here

Example of how my hierarchy wants to be:
enter image description here

答案1

得分: 0

根据您的评论,要获取与特定用户对应的仅 Task 对象,请使用以下代码行:

String userId = "User-13294";
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference tasksRef = rootRef.child("Users").child(userId).child("Tasks");
ValueEventListener valueEventListener = new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        for(DataSnapshot taskSnapshot : dataSnapshot.getChildren()) {
            String title = taskSnapshot.child("title").getValue(String.class);
            Log.d("TAG", title);
        }
    }

    @Override
    public void onCancelled(@NonNull DatabaseError databaseError) {
        Log.d("TAG", databaseError.getMessage()); // 不要忽略潜在的错误!
    }
};
tasksRef.addListenerForSingleValueEvent(valueEventListener);

因为 User-13294 只有一个 Task,logcat 中的结果将只有一个标题:

Swimming
英文:

According to your comments, to get only the Task objects the correspond to a particular user, please use the following lines of code:

String userId = "User-13294";
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference tasksRef = rootRef.child("Users").child(userId).child("Tasks");
ValueEventListener valueEventListener = new ValueEventListener() {
	@Override
	public void onDataChange(DataSnapshot dataSnapshot) {
		for(DataSnapshot taskSnapshot : dataSnapshot.getChildren()) {
			String title = taskSnapshot.child("title").getValue(String.class);
			Log.d("TAG", title);
		}
	}

	@Override
	public void onCancelled(@NonNull DatabaseError databaseError) {
		Log.d("TAG", databaseError.getMessage()); //Don't ignore potential errors!
	}
};
tasksRef.addListenerForSingleValueEvent(valueEventListener);

Because User-13294 has only one Task, the result in the logcat will be only one title:

Swimming

huangapple
  • 本文由 发表于 2020年9月21日 22:55:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/63994818.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定