一切工作正常,我认为只是userDataModel.setUserRating(aFloat / (int) dataSnapshot.getChildrenCount());不能提供价值。

String.valueof(aFloat / (int) dataSnapshot.getChildrenCount()));
也给出正确的值。

FirebaseDatabase.getInstance().getReference().child("Users").child(userDataModel.getUserID()).child("Ratings")
                        .addListenerForSingleValueEvent(new ValueEventListener() {
                            @Override
                            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                                for (DataSnapshot dataSnapshot1 : dataSnapshot.getChildren()) {
                                    userDataModel1 = dataSnapshot.getValue(UserDataModel.class);
                                    aFloat = aFloat + Float.parseFloat(Objects.requireNonNull(dataSnapshot1.getValue()).toString());
                                }
                                userDataModel.setUserRating(aFloat / (int) dataSnapshot.getChildrenCount());
                            }

                            @Override
                            public void onCancelled(@NonNull DatabaseError databaseError) {

                            }
                        });


UserDataModel

public class UserDataModel {
    float userRating;

    public float getUserRating() {
        return userRating;
    }

    public void setUserRating(float userRating) {
        this.userRating = userRating;
    }
}


图片网址:https://i.stack.imgur.com/t14Cc.png

最佳答案

发生这种情况可能是因为userDataModel.setUserRating(aFloat / (int) dataSnapshot.getChildrenCount());返回null,所以您没有获得值

关于java - Firebase值未更新,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/61555963/

10-10 03:24