DataSnapShot对象的值返回0而不是实际值

时间:2019-08-20 18:51:46

标签: android firebase-realtime-database

我正在从在线教程中进行实时跟踪应用程序,这里我使用Firebase设置状态系统。但是它返回0而不是实际值:

我的Firebase图片:

enter image description here

这是我的问题课:

 String question  ;
int level , operator  , answer;

public question() {

}

public question(int answer, int level, int operator , String question) {
    this.question = question;
    this.answer = answer;
    this.level = level;
    this.operator = operator;
}

public String getQuestion() {
    return question;
}

public void setQuestion(String question) {
    this.question = question;
}

public int getAnswer() {
    return answer;
}

public void setAnswer(int answer) {
    this.answer = answer;
}

public int getLevel() {
    return level;
}

public void setLevel(int level) {
    this.level = level;
}

public int getOperator() {
    return operator;
}

public void setOperator(int operat) {
    this.operator = operator;
}

@Override
public String toString() {
    return  question + " =" ;
}

主要活动:

List<question> questions ;
FirebaseDatabase database;
DatabaseReference myRef;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_opertion);
    questions = new ArrayList<>();
    questions = readFromFireBase();
    setUpClick();

} 

private void setUpClick() {
btnbuttonThatSet.setOnClickListener(this);
    btnbuttonOk.setOnClickListener(this);

}


@Override
public void onClick(View view) {

case R.id.buttonOk:


            theRealAnswer =theAnswer.getText().toString();
            correctAnswer = questions.get(questionNumber).answer;
            inputQuestionOperator = questions.get(questionNumber).operator;



            //this is for check 
            Log.d(" the question",questions.get(questionNumber).question+
                    " answer : "+questions.get(questionNumber).answer +
                    " operator : "+questions.get(questionNumber).operator +
                    " level : "+questions.get(questionNumber).level);





            }
 }




public ArrayList<question> readFromFireBase(){
        ArrayList<question>arrayList = new ArrayList<>() ;
        database = FirebaseDatabase.getInstance();
        myRef = database.getReference("Questions");
        myRef.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                // This method is called once with the initial value and again
                // whenever data at this location is updated.
                for (DataSnapshot postSnapshot: dataSnapshot.getChildren()) {
                    Log.d("", "Value is: " + postSnapshot.getValue(question.class).operator);
                    questions.add(postSnapshot.getValue(question.class));


            }
            questionNumber = getRandomElement(questions);
            textView2.setText(questions.get(questionNumber).question);
             Log.d("", "Value is: " + questions.get(questionNumber).operator);
        }

        @Override
        public void onCancelled(DatabaseError error) {
            // Failed to read value
            // Log.w(TAG, "Failed to read value.", error.toException());
        }
    });
    return arrayList;
}




// Function select an element base on index
// and return an element
public int getRandomElement(List<question> list)
{
    Random rand = new Random();
    return rand.nextInt(list.size());
}

这是使用firebase运行时运行代码后的结果:

enter image description here

它必须是同一件事..所有数据都没有任何问题(question,answer,level),除了运算符始终为0且firebase中的数据有此错误

1 个答案:

答案 0 :(得分:0)

您在setOperator method上设置了错误的值。我猜你打错了

setOperator(int operat){
  this.operator = operator;
} 

代替

setOperator(int operator){
  this.operator = operator;
}