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

标签 android firebase-realtime-database

我正在根据在线教程制作一个实时跟踪应用程序,在这里我使用 firebase 设置在线状态系统。但它返回 0 而不是实际值:

来 self 的 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 + " =" ;
}

主要 Activity :

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 runtime 运行代码后的结果:

enter image description here

它必须是同一件事.. 所有数据都没有任何问题(问题、答案、级别),除了运算符始终为 0 并且 firebase 中的数据有问题

最佳答案

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

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

代替

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

关于android - DataSnapShot 对象的值返回 0 而不是实际值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57579937/

相关文章:

android - 如何使用 .apk 文件运行(不仅是安装)android 应用程序?

Android 在 Dialog 中覆盖 onKeyDown

java - 如何使用 Firebase 数据库和不同的 ViewHolder 根据类型(视频、文本、图像)恢复数据?

javascript - 在 Firebase onWrite 中删除云函数中的当前节点

swift - 在Firebase中构建用户数据库模型

android - Webview html输入表单不显示/不允许键盘

android - 在后台单击通知时打开特定 Activity

android - 微调器模式下的 TimePickerDialog

swift - 基于 firebase 数据的负载选择器

node.js - 在哪里上传 Firebase Node.js 客户端?