我试图从我的Firebase数据库中获取posts
和uid
。我用这个:
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
一次又一次地被提取。如何在一次迭代中停止?
答案 0 :(得分:0)
Long num = dataSnapshot.getChildrenCount();
将此行代码移到for循环外。