我已将问题作为问题的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;
}
}
将此添加到您的问题模型班
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;
}
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。