仅从当前用户阅读Firebase - Android

时间:2018-03-10 20:52:43

标签: java android firebase

我只是为只登录系统的当前用户读取数据时遇到问题。目前,它正在阅读" team"的所有用户数据。进入listView。

对于上下文,这是一个体育应用程序,其中团队的名称被读入listView。

这是我的代码:

databaseReference.addValueEventListener(new ValueEventListener() {
    ArrayList<Object> array = new ArrayList<>();
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        Log.i("snap", "onDataChange: " + dataSnapshot.getValue());

        for(DataSnapshot ds: dataSnapshot.getChildren()) {
            String userId = firebaseAuth.getCurrentUser().getUid();
            String idKey = ds.getKey();
            for(DataSnapshot team: ds.getChildren()) {
                String key = team.getKey();
                Log.i("my team", team.toString());
                array.add(key);
            }
        }
        ArrayAdapter adapter = new ArrayAdapter(StatisticsActivity.this, android.R.layout.simple_list_item_1, array);
        listViewTeam.setAdapter(adapter);
        dataSnapshot.getValue();
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {
    }
});

如果有人可以提供帮助,我们将不胜感激。

1 个答案:

答案 0 :(得分:0)

查看带注释的代码

//get firebase user id as you did before
    String userId = firebaseAuth.getInstance().getCurrentUser().getUid();

    //get reference and put there the name of the table where you have users
    DatabaseReference ref = FirebaseDatabase.getInstance().getReference(USERS_TABLE).child(userId);


    //grab info
    ref.addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
           //and here you receive the logged in user
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {
        }
    });