我将一些数据存储在Firebase实时数据库中并创建问题类。我还创建了commonmodel类。 这是表演测验活动:
public class ShowQuizActivity extends AppCompatActivity implements View.OnClickListener {
final static int INTERVAL = 1000;
final static int TIMEOUT = 7000;
int progressvalue = 0;
CountDownTimer mCountDown;
int index = 0,score = 0,thisQuestion = 0,totalQuestion,correctAnswer;
FirebaseDatabase database;
DatabaseReference questions;
ProgressBar progressBar;
Button btnA,btnB,btnC,btnD;
TextView txtQuestion,txtquestionNum,txtScore;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_show_quiz);
database = FirebaseDatabase.getInstance();
questions = database.getReference("Question");
txtQuestion = findViewById(R.id.question);
txtquestionNum = findViewById(R.id.quNum);
txtScore = findViewById(R.id.score);
progressBar = findViewById(R.id.progressBar);
btnA = findViewById(R.id.option1);
btnB = findViewById(R.id.option2);
btnC = findViewById(R.id.option3);
btnD = findViewById(R.id.option4);
btnA.setOnClickListener(this);
btnB.setOnClickListener(this);
btnC.setOnClickListener(this);
btnD.setOnClickListener(this);
loadQuestion(CommonModel.question);
}
private void loadQuestion(List<Question> question) {
if (CommonModel.question.size() > 0)
CommonModel.question.clear();
}
这是我的CommonModel类:
import java.util.ArrayList;
import java.util.List;
public class CommonModel {
public static SignInUpModel currentUser;
public static List<Question> question = new ArrayList<>();
}
问题课在这里:
public class Question {
private String answer,option1,option2,option3,option4,question;
public Question() {
}
public Question(String answer, String option1, String option2, String option3, String option4, String question) {
this.answer = answer;
this.option1 = option1;
this.option2 = option2;
this.option3 = option3;
this.option4 = option4;
this.question = question;
}
public String getAnswer() {
return answer;
}
public void setAnswer(String answer) {
this.answer = answer;
}
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 getQuestion() {
return question;
}
public void setQuestion(String question) {
this.question = question;
}
}
数据库结构:
我无法在我的应用中显示此数据。我该怎么办?
答案 0 :(得分:1)
根据您的评论,要获取所有问题,请使用以下代码行:
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference questionRef = rootRef.child("Question");
ValueEventListener valueEventListener = new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
List<Question> questionList = new ArrayList<>();
for(DataSnapshot ds : dataSnapshot.getChildren()) {
Question q = ds.getValue(Question.class);
questionList.add(q);
String answer = q.getAnswer();
String option1 = q.getOption1();
String option2 = q.getOption2();
String option3 = q.getOption3();
String option4 = q.getOption3();
String question = q.getQuestion();
Log.d(TAG, answer + "/" + option1 + "/" + option2 + "/" + option3 + "/" + option4 + "/" + question);
}
//Do what you need to do with your list
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
Log.d(TAG, databaseError.getMessage()); //Don't ignore errors!
}
};
questionRef.addListenerForSingleValueEvent(valueEventListener);