在另一个RealmObject中使用RealmObject的对象

时间:2019-01-11 14:16:11

标签: java realm

我正在尝试使用Realm开发一个移动应用程序,在其中将问题及其选项存储在数据库中。

我创建了三个数据模型来保存问题,答案和选项。问题中包含答案及其选项。我添加了问题,但在添加答案和选项时遇到了挑战。这是我的下面的代码

    public class Questions extends RealmObject implements Observable, RealmDataBinding {

    private int id;

    private String question;

    private Answers answers;

    private RealmList<Options> options;

    public int getId() { return id; }

    public void setId(int id) { this.id = id; }

    public String getQuestion() { return question; }

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

    public Answers getAnswers() { return answers; }

    public void setAnswers(Answers answers) { this.answers = answers; }

    public RealmList<Options> getOptions() { return options; }

    public void setOptions(RealmList<Options> options) { this.options = options; }

    @Ignore
    private transient PropertyChangeRegistry mCallbacks;

    // from observable interface
    @Override
    public synchronized void
        addOnPropertyChangedCallback(OnPropertyChangedCallback callback) {
        if (mCallbacks == null) {
            mCallbacks = new PropertyChangeRegistry();
        }
        mCallbacks.add(callback);
    }

    // from observable interface
    @Override
    public synchronized void removeOnPropertyChangedCallback(OnPropertyChangedCallback callback) {
        if (mCallbacks != null) {
            mCallbacks.remove(callback);
        }
    }

    @Override
    public void notifyChange() {
        if (mCallbacks != null) {
            mCallbacks.notifyCallbacks(this, 0, null);
        }
    }

    public void notifyPropertyChanged(int fieldId) {
        if (mCallbacks != null) {
            mCallbacks.notifyCallbacks(this, fieldId, null);
        }
    }
    }

    public class Options extends RealmObject {

    private String option1, option2, option3, option4;

    public String getOption1() { return option1; }

    public void setOption1(String options) { 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 class Answers extends RealmObject {

    public String answer;

    public String getAnswer() { return answer; }

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

public class GameActivity extends AppCompatActivity {

    private Realm realm;

    private GameMainBinding binding;
    private Context context;

    private Questions questions;
    private Answers answers;
    private Options options;

    private static final String TAG = GameActivity.class.getSimpleName();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this, R.layout.game_main);
        context = GameActivity.this;
        realm = Realm.getDefaultInstance();
        createNewDatabase();
        questions = realm.where(Questions.class).findFirst();
        questions.addChangeListener(RealmDataBinding.FACTORY.create());
    }

    @Override
    protected void onStart() {
        super.onStart();
        binding.setQuest(questions);
        binding.setOpt(options);
    }

    private void createNewDatabase() {

        final String[] questionsList = {"What is the square of 2?",
            "What is the cube of 3?",
            "What is the square root of 4?"};

        final String[] answersList = {"4", "27", "2"};

        for(int i = 0; i < questionsList.length; i++){
            String tempQuest = questionsList[i];
            String tempAns = answersList[i];

            addQuestion((i+1), tempQuest);
            addAnswer(tempAns);
            createOptions((i+1), tempAns, i);
        }
    }

    private void createOptions(int questNo,String ans, int n) {
        realm.executeTransactionAsync(realm1 -> {
            options = realm1.createObject(Options.class);
            options.setOption1(String.valueOf(randomize(n)));
            options.setOption2(ans);
            options.setOption3(String.valueOf(randomize(n)));
            options.setOption4(String.valueOf(randomize(n)));
        });
    }

    private void addAnswer(String ans) {
        realm.executeTransactionAsync(realm1 -> {
             answers = realm1.createObject(Answers.class);
            answers.setAnswer(ans);
        });
    }

    private void addQuestion(int questNo, String quest){
        realm.executeTransaction(realm1 -> {
            questions = realm1.createObject(Questions.class);
            questions.setId(questNo);
            questions.setQuestion(quest);
            questions.setAnswers(answers);
            questions.getOptions().add(options);
        });
    }
}

我希望能够为问题添加选项

0 个答案:

没有答案