开发者社区> 问答> 正文

为什么Firebase中的数据没有显示在我的文本视图中?

我已将问题作为问题的json数组发布在firebase实时数据库上,并尝试将其从firebase检索到我的应用程序,但是我在文本视图中看到的文本与我在xml文件中设置为默认值的文本相同,所以请帮助我的数据库是一个JSON数组,包含一个Questions类,其中有5个类别,每个类别有9个问题域,其中有6个部分; 问题,四个选项和正确答案(所有字符串)


public class Questions extends AppCompatActivity {
DatabaseReference dref;
DatabaseReference ref,ques;
TextView question,option1,option2,option3,option4;
ImageButton imageButton;

int val,correct,wrong,score;

public Questions() {
    this.val = 0;
    this.correct=0;
    this.wrong=0;
    this.score=0;
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_questions);
    question=findViewById(R.id.question);
    imageButton=findViewById(R.id.next);
    option1=findViewById(R.id.opt1);
    option2=findViewById(R.id.opt2);
    option3=findViewById(R.id.opt3);
    option4=findViewById(R.id.opt4);
    dref= FirebaseDatabase.getInstance().getReference();
    Intent intent=getIntent();
    Integer value=intent.getIntExtra("type",0);
   switch (value)
   {
       case 1:
           ref=dref.child("Questions").child("Bollywood");

            break;
       case 2:
           ref=dref.child("Questions").child("Sports");
           break;
       case 3:
           ref=dref.child("Questions").child("Technology");
           break;
       case 4:
           ref=dref.child("Questions").child("Entertainment");
           break;
       case 5:
           ref=dref.child("Questions").child("Miscellaneous");
           break;
           default:
               break;
   }
    ques = ref.child(String.valueOf(val));
    ques.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
            QAnswer qAnswer=dataSnapshot.getValue(QAnswer.class);
            question.setText(qAnswer.getQuestion());
            option1.setText(qAnswer.getOption1());
            option2.setText(qAnswer.getOption2());
            option3.setText(qAnswer.getOption3());
            option4.setText(qAnswer.getOption4());
        }

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

        }
    });
   imageButton.setOnClickListener(new View.OnClickListener() {
       @Override
       public void onClick(View v) {
           if(val>15)
           {

           }
           else {
               ques = ref.child(String.valueOf(++val));
               ques.addValueEventListener(new ValueEventListener() {
                   @Override
                   public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                       final QAnswer qAnswer=dataSnapshot.getValue(QAnswer.class);
                       Log.d("value",qAnswer.getQuestion()+qAnswer.getAnswer());
                       question.setText(qAnswer.getQuestion());
                       option1.setText(qAnswer.getOption1());
                       option2.setText(qAnswer.getOption2());
                       option3.setText(qAnswer.getOption3());
                       option4.setText(qAnswer.getOption4());
                       option1.setOnClickListener(new View.OnClickListener() {
                           @Override
                           public void onClick(View v) {
                               if(qAnswer.getOption1()==qAnswer.getAnswer())
                               {
                                   option1.setBackgroundColor(getResources().getColor(R.color.colorPrimary));
                                   correct++;
                               }
                               else {
                                   option1.setBackgroundColor(getResources().getColor(R.color.colorAccent));
                                   wrong++;

                               }
                           }
                       });
                       option2.setOnClickListener(new View.OnClickListener() {
                           @Override
                           public void onClick(View v) {

                           }
                       });
                       option3.setOnClickListener(new View.OnClickListener() {
                           @Override
                           public void onClick(View v) {

                           }
                       });
                       option4.setOnClickListener(new View.OnClickListener() {
                           @Override
                           public void onClick(View v) {

                           }
                       });
                   }

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

                   }
               });

           }
       }
   });


}
}

这是我的问题模型类:

public class QAnswer {
String question;
String option1;
String option2;
String option3;
String option4;
String answer;

public QAnswer(String question, String option1, String option2, String option3, String option4, String answer) {
    this.question = question;
    this.option1 = option1;
    this.option2 = option2;
    this.option3 = option3;
    this.option4 = option4;
    this.answer = answer;
}

public String getQuestion() {
    return question;
}

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

public String getOption1() {
    return option1;
}

public void setOption1(String option1) {
    this.option1 = option1;
}

public String getOption2() {
    return option2;
}

public void setOption2(String option2) {
    this.option2 = option2;
}

public String getOption3() {
    return option3;
}

public void setOption3(String option3) {
    this.option3 = option3;
}

public String getOption4() {
    return option4;
}

public void setOption4(String option4) {
    this.option4 = option4;
}

public String getAnswer() {
    return answer;
}

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

展开
收起
Puppet 2020-01-18 18:58:34 760 0
1 条回答
写回答
取消 提交回答
  • 将此添加到您的问题模型班

    
    public QAnswer(){
    }
    

    在这之前

    public QAnswer(String question, String option1, String option2, String option3, String option4, String answer) {
        this.question = question;
        this.option1 = option1;
        this.option2 = option2;
        this.option3 = option3;
        this.option4 = option4;
        this.answer = answer;
    }
    
    2020-01-18 18:58:55
    赞同 展开评论 打赏
问答分类:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
数据展现:可视化报表及嵌入应用 立即下载
低代码开发师(初级)实战教程 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载