我正在制作一个Android测验应用。从我的数据库中提取问题。我正在解析从php收到的数据并将其保存在数据库中,php文件工作正常。对于解析我正在使用Volley和存储模型问题,在配置了默认实例的Realm数据库中有六个字段“question”,“optionA”,“optionB”,“optionC”,“optionD”和“correct”。数据存储得很好,但是当我试图访问存储在RealmList中的模型问题的数据库时,它只是将最后一个值插入到数据库中。我搜索了解决方案,在SO的答案中,提到了{{3}}。但这不起作用,我得到一个异常,它没有登录到android studio中的logcat。请帮忙。
以下是我到目前为止所做的代码..
Questions.java
public class Questions extends RealmObject {
public RealmList<ModelQuestion> questionsList=new RealmList<>();
public void addQuestion(ModelQuestion modelQuestion){
questionsList.add(modelQuestion);
}
public RealmList<ModelQuestion> getQuestionsList(){
return this.questionsList;
}
}
ModelQuestion.java
public class ModelQuestion extends RealmObject {
private String question;
private String optionA;
private String optionB;
private String optionC;
private String optionD;
private String correct;
private RealmList < ModelQuestion > modelQuestionList;
public String getCorrect() {
return correct;
}
public void setCorrect(String correct) {
this.correct = correct;
}
public String getOptionA() {
return optionA;
}
public void setOptionA(String optionA) {
this.optionA = optionA;
}
public String getOptionB() {
return optionB;
}
public void setOptionB(String optionB) {
this.optionB = optionB;
}
public String getOptionC() {
return optionC;
}
public void setOptionC(String optionC) {
this.optionC = optionC;
}
public String getOptionD() {
return optionD;
}
public void setOptionD(String optionD) {
this.optionD = optionD;
}
public String getQuestion() {
return question;
}
public void setQuestion(String question) {
this.question = question;
}
public RealmList < ModelQuestion > getModelQuestionList() {
return modelQuestionList;
}
public void setModelQuestionList(RealmList < ModelQuestion > modelQuestionList) {
this.modelQuestionList = modelQuestionList;
}
}
关于存储的所有内容的功能的主要部分正在发生..
try {
JSONObject jsonObject = new JSONObject(response);
JSONArray jsonArray = jsonObject.optJSONArray("questions");
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject1 = jsonArray.optJSONObject(i);
question = jsonObject1.optString("question", "Question not available.");
optionA = jsonObject1.optString("optionA", "Option unavailable");
optionB = jsonObject1.optString("optionB", "Option unavailable");
optionC = jsonObject1.optString("optionC", "Option unavailable");
optionD = jsonObject1.optString("optionD", "Option unavailable");
correct = jsonObject1.optString("correct", "X");
realm.executeTransactionAsync(new Realm.Transaction() {
@Override
public void execute(Realm realm) {
if (!dbInitialized) {
questions = realm.createObject(Questions.class);
dbInitialized = true;
}
ModelQuestion modelQuestion = new ModelQuestion();
modelQuestion.setQuestion(question);
modelQuestion.setOptionA(optionA);
modelQuestion.setOptionB(optionB);
modelQuestion.setOptionC(optionC);
modelQuestion.setOptionD(optionD);
modelQuestion.setCorrect(correct);
ModelQuestion mq = realm.copyToRealm(modelQuestion);
questions.getQuestionsList().add(mq);
//Toast.makeText(MainActivity.this,questions.getQuestionsList().size()+"",Toast.LENGTH_SHORT).show();
}
}, new Realm.Transaction.OnSuccess() {
@Override
public void onSuccess() {
progressDialog.dismiss();
new AppPreferences().setDownloadComplete(context, true);
}
}, new Realm.Transaction.OnError() {
@Override
public void onError(Throwable error) {
}
});
}
} catch (JSONException e) {
e.printStackTrace();
new AppPreferences().setDownloadComplete(context, false);
Intent intent = new Intent(MainActivity.this, PlayGround.class);
startActivity(intent);
}
我从Realm获取数据:
RealmResults < Questions > questionDB = realm.where(Questions.class).findAll();
// for(int i=0;i<questionDB.size();i++){
RealmList < ModelQuestion > mod = questionDB.get(0).getQuestionsList();
for (int j = 0; j < mod.size(); j++) {
qu += mod.get(j).getQuestion() + "\n";
Toast.makeText(context, qu, Toast.LENGTH_SHORT).show();
}
// }
questionsTV.setText(qu);
Realm在MyApplication类中配置了默认实例:
public class MyApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
Realm.init(this);
RealmConfiguration config = new RealmConfiguration.Builder().build();
Realm.setDefaultConfiguration(config);
}
}