private void updateScoreBreaker(){
    DBref=FirebaseDatabase.getInstance().getReference().child(Gamelevel);
    Query maxScore =DBref.orderByChild(SCORE).limitToLast(1);
    maxScore.addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
         maxScoreLevel=Integer.parseInt(dataSnapshot.child(SCORE).getValue(String.class));
              maxScoreName=dataSnapshot.getKey();
        }
        @Override
        public void onCancelled(DatabaseError databaseError) {
        }
    });
}


我有这个错误:
找不到局部变量'maxScoreLevel'

最佳答案

似乎您尚未声明maxScoreLevel。您应该将其声明为类的属性,以便可以从ValueEventListener中更改其值。

class something {
    ...
    Integer maxScoreLevel;
    String maxScoreName;
    ...

    private void updateScoreBreaker() {
        DBref = FirebaseDatabase.getInstance().getReference().child(Gamelevel);
        Query maxScore = DBref.orderByChild(SCORE).limitToLast(1);
        maxScore.addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                maxScoreLevel = dataSnapshot.child(SCORE).getValue(Integer.class);
                maxScoreName = dataSnapshot.getKey();
            }

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


如果相应的引用不存在或没有值,则将为maxScoreLevel分配空值

关于android - 如何从Firebase检索int,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/46330463/

10-09 09:17