Firebase会返回重复值

时间:2016-10-06 03:28:07

标签: android firebase firebase-realtime-database

我试图从我的Firebase数据库中获取postsuid。我用这个:

mPostReference = FirebaseDatabase.getInstance().getReference()
            .child("posts");
mPostReference.addListenerForSingleValueEvent(
        new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
               // Get Post object and use the values to update the UI

                for (DataSnapshot postSnapshot: dataSnapshot.getChildren()) {
                    String uid = (String) postSnapshot.child("uid").getValue();
                    String body = (String) postSnapshot.child("body").getValue();
                    Long num = dataSnapshot.getChildrenCount();
                    Toast.makeText(getActivity(), body, Toast.LENGTH_SHORT).show();
                    Toast.makeText(getActivity(), num.toString(),  Toast.LENGTH_SHORT).show();


                }
            }

我将num值设为3,但我获得的数据是重复的。相同的body& uid一次又一次地被提取。如何在一次迭代中停止?

My Database in firebase

1 个答案:

答案 0 :(得分:0)

Long num = dataSnapshot.getChildrenCount();

将此行代码移到for循环外。