如何在对象中保存测验应用的答案并检索

时间:2018-03-28 05:31:52

标签: android object radio-button

我正在开发一个包含测验功能的学习应用程序。我可以检索问题,也可以保存在对象中。我有一个下一个和上一个按钮来查看下一个或上一个问题。它也工作正常。但我需要你帮助保存对象中的选定答案。如果用户按下上一个按钮,则应在单选按钮中选择所选答案。如果用户更改了所选答案,那么它也应该在对象中更新。

TestQuestion

 ArrayList<QuestionObject> questionObject;
 ArrayList<String> answerObject;
 int position = 0;

 if (position == 0){
        btnPrev.setVisibility(View.INVISIBLE);
    }

    btnNext.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            position = position+1;
          //  txtQuestionNo.setText(String.valueOf(questionObject.get(position).getId()));
            txtQuestionNo.setText(String.valueOf(position+1));
            txtQuestion.setText(questionObject.get(position).getQuestion());
            optionA.setText(questionObject.get(position).getOption1());
            optionB.setText(questionObject.get(position).getOption2());
            optionC.setText(questionObject.get(position).getOption3());
            optionD.setText(questionObject.get(position).getOption4());

            if (position>0){
                btnPrev.setVisibility(View.VISIBLE);
            }
            if (position+1 == questionObject.size()){
                btnNext.setVisibility(View.INVISIBLE);
            }



        }
    });
    btnPrev.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            position = position-1;
          //  txtQuestionNo.setText(String.valueOf(questionObject.get(position).getId()));
            txtQuestionNo.setText(String.valueOf(position+1));
            txtQuestion.setText(questionObject.get(position).getQuestion());
            optionA.setText(questionObject.get(position).getOption1());
            optionB.setText(questionObject.get(position).getOption2());
            optionC.setText(questionObject.get(position).getOption3());
            optionD.setText(questionObject.get(position).getOption4());


            if (position == 0){
                btnPrev.setVisibility(View.INVISIBLE);
            }
            if (position+1 != questionObject.size()){
                btnNext.setVisibility(View.VISIBLE);
            }


        }
    });
    btnCancel.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            startActivity(new Intent(TestQuestion.this, TestResult.class));
        }
    });

    radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            answerObject = new ArrayList<>();
            String ans = "";
            switch (checkedId){
                case R.id.optionA:
                    ans = "A";
                    break;
                case R.id.optionB:
                    ans = "B";
                    break;
                case R.id.optionC:
                    ans = "C";
                    break;
                case R.id.optionD:
                    ans = "D";
                    break;
            }

            answerObject.add(ans);


        }
    });

QuestionObject

public class QuestionObject {
private int id;
private String question;
private String option1;
private String option2;
private String option3;
private String option4;


public QuestionObject(JSONObject jsonObject){
    if (jsonObject == null) return;
    this.id = jsonObject.optInt("id");
    this.question = jsonObject.optString("Question");
    this.option1 = jsonObject.optString("option1");
    this.option2 = jsonObject.optString("option2");
    this.option3 = jsonObject.optString("option3");
    this.option4 = jsonObject.optString("option4");
}

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 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;
} }

2 个答案:

答案 0 :(得分:0)

我不知道你的questionObject对象是怎样的。

您有一个变量position,您可以在其中存储在listner方法之外已知的当前所选对象索引。

您可以这样做:

radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        answerObject = new ArrayList<>();
        String ans = "";
        switch (checkedId){
            case R.id.optionA:
                ans = "A";
                break;
            case R.id.optionB:
                ans = "B";
                break;
            case R.id.optionC:
                ans = "C";
                break;
            case R.id.optionD:
                ans = "D";
                break;
        }

        answerObject.add(ans);
        questionObject.get(position).getQuestion().setAnswer(answerObject); // I added this line

    }
});

查看此行:

questionObject.get(position).setAnswer(answerObject); 

当然,假设您的questionObject具有方法setAnswer(ArrayList<> answer)

<强>更新

如果无法修改questionObject,请创建自定义answerObject。

public class AnswerObject {
     private int questionId;
     private String answer;

     public AnswerObject (int questionId, String answer){

         this.questionId = questionId;
         this.answer = answer;
     }

 }

// Outside the listeners
ArrayList<AnswerObject> answerObjects = new ArrayList<>();


// Inside your setOnCheckedChangeListener
AnswerObject answerObject = new AnswerObject(questionObject.get(position).getId(), ans);
answerObjects.add(answerObject);

测试完成后,您可以使用answerObjects数组将所有答案发布到API端点{/ 1}}。

答案 1 :(得分:0)

以最简单的形式(如果您不想使用数据库等),我建议Gson和SharedPreferences保存和恢复您的数据。

<android.support.v7.widget.AppCompatTextView
    android:paddingTop="12dp"
    android:paddingStart="16dp"
    android:paddingEnd="16dp"
    android:paddingBottom="12dp"

    android:hint="Apparently..."
    android:textColorHint="#BDBDBD"

    android:textSize="16sp"
    android:textColor="#000000"

    android:layout_width="match_parent"
    android:layout_height="wrap_content"/>

当您想保存时:

compile 'com.google.code.gson:gson:xx'
SharedPreferences  prefs = getPreferences(MODE_PRIVATE);

当您想要恢复数据时:

Editor prefsEditor = prefs.edit();
Gson gson = new Gson();
String json = gson.toJson(theObjectYouWantToSave);
prefsEditor.putString("MyObject", json);
prefsEditor.commit();