Fragment editText data getting cleared when i am moving to another activity and coming back in Quiz app

…衆ロ難τιáo~ 提交于 2020-06-09 05:32:48

问题


I have an android quiz app source code. In which options are in checkbox type. But for some questions, I need to make as edittext answer. How to convert the input type from checkbox to edittext. I will attach the images explaining the requirement. I am giving the GitHub link below

Source Code of the Quiz App:

https://github.com/tendentious/Multiple-Choice-Questions-Android-App

XML files:

https://github.com/tendentious/Multiple-Choice-Questions-Android-App/tree/master/app/src/main/res/layout

Questions Activity:

https://github.com/tendentious/Multiple-Choice-Questions-Android-App/tree/master/app/src/main/java/com/Data

Fragment and Quiz activity:

https://github.com/tendentious/Multiple-Choice-Questions-Android-App/tree/master/app/src/main/java/com/QuizActivity

Apk File Path:

https://github.com/tendentious/Multiple-Choice-Questions-Android-App/blob/master/app/build/outputs/apk/app-debug.apk

Please Help me with this...

I modified Question.java and MyServerData.java file as shown below(for adding the correct answer)

Question.java:

public class Question {
    private String question;
    private String answer;
    private String[] answerText;
    private Boolean[] correctAnswer;

    private Boolean[] checked = new Boolean[3];

    public Question(String question, String answer, String[] answerText, Boolean[] correctAnswer){
        this.question = question;
        this.answer = answer;
        this.answerText = answerText.clone();
        this.correctAnswer = correctAnswer.clone();
        this.resetChecked();
    }
    public void setChecked(int number,boolean state){
        if(number>=0 && number <=2){
            checked[number] = state;
        }
    }
    public void resetChecked(){
        Arrays.fill(checked,false);
    }
    public Boolean isChecked(int number) {
        return checked[number];
    }
    public Boolean isCorrectAnswer(int number){
        return  correctAnswer[number];
    }
    public Boolean[] getUserAnswers() {
        return checked;
    }
    public Boolean[] getAllCorrectAnswers(){
        return correctAnswer;
    }
    public String[] getAllAnswersText(){
        return answerText;
    }
    public String getQuestionText() {
        return question;
    }
    public String getAnswerText() {
        return answer;
    }
}

MyServerData.java

public class MyServerData {
    private static MyServerData myInstance = null;
    private static LinkedHashMap<String, Object> myCategories;
    private int totalQuestions;

    //test states: notStarted, inProgress, finished
    private static String testState = "notStarted";


    protected MyServerData() {
        //initialize questions from server, shared preferences, files etc.
        //questions are hardcoded here for testing
        Question[] FirstCategory  = new Question[3];;
        Question[] SecondCategory = new Question[3];;
        String q = "What is the capital of France ?";
        String b = "Paris";
        String[] a = new String[3];
        Boolean[] r = new Boolean[3];
        a[0] = "Paris";
        a[1] = "Prague";
        a[2] = "Berlin";
        r[0] = true;
        r[1] = false;
        r[2] = false;

        Question question = new Question(q,a,r);
        FirstCategory[0] = question;
        q = "Which of the following is an OS ?";
        b = "LINUX";
        a[0] = "PHP";
        a[1] = "LINUX";
        a[2] = "WINDOWS";
        r[0] = false;
        r[1] = true;
        r[2] = true;
        question = new Question(q,a,r);
        FirstCategory[1] = question;
        q = "What number is the highest ?";
        b = "18";
        a[0] = "12";
        a[1] = "14";
        a[2] = "18";
        r[0] = false;
        r[1] = false;
        r[2] = true;
        question = new Question(q,a,r);
        FirstCategory[2] = question;
        q = "Which of the following is flying ?";
        b = "Bird";
        a[0] = "Bird";
        a[1] = "Lion";
        a[2] = "Bee";
        r[0] = true;
        r[1] = false;
        r[2] = true;
        SecondCategory[0] = new Question(q,a,r);
        q = "What material is the strongest ?";
        b = "Diamond";
        a[0] = "Wood";
        a[1] = "Metal";
        a[2] = "Diamond";
        r[0] = false;
        r[1] = false;
        r[2] = true;
        SecondCategory[1] = new Question(q,a,r);q = "What number is the lowest ?";
        b = "11";
        a[0] = "15";
        a[1] = "11";
        a[2] = "12";
        r[0] = false;
        r[1] = true;
        r[2] = false;
        SecondCategory[2] = new Question(q,a,r);

        myCategories = new LinkedHashMap<String,Object>();
        myCategories.put("FirstCategory", FirstCategory);
        myCategories.put("SecondCategory", SecondCategory);
        for(String entry: myCategories.keySet()){
            totalQuestions += ((Question[])myCategories.get(entry)).length;
        }
    }

And finally I modified QuestionFragment.java like this:

public class QuestionFragment extends Fragment {

    LinearLayout answersContainer;
    int currentPageNr;

    @Override
    public View onCreateView(LayoutInflater lInflater, ViewGroup container,Bundle saveInstanceState){
        currentPageNr = getArguments().getInt("position");
        //initialize some variables
        final Question currentQuestion = MyServerData.getInstance().getQuestion(currentPageNr);
        View rootView = lInflater.inflate(R.layout.quiz_activity_fragment,container,false);
        //initialize questionText
        TextView question = (TextView) rootView.findViewById(R.id.questionText);
        question.setMovementMethod(new ScrollingMovementMethod());
        question.setText(currentQuestion.getQuestionText());

        //initialize correct answer from
        String correctAnswer = currentQuestion.getAnswerText();

        //initialize user input
        EditText et1 = (EditText) rootView.findViewById(R.id.et1);
        EditText et2 = (EditText) rootView.findViewById(R.id.et2);
        String input = et1.getText().toString().trim();

        //comparing both the strings
        if(MyServerData.getInstance().getTestState().equals("finished") ){
            et1.setEnabled(false);
            et2.setEnabled(false);
            if(correctAnswer.matches(et1.getText().toString().trim|())) {
                et1.setText(input)
                et1.setTextColor(Color.parseColor("#4DAD47"));
                et2.setText("Correct Answer: " + correctAnswer)             
            }else {
                et1.setText(input)
                et1.setTextColor(Color.parseColor("#CE0B0B"));
                et2.setText("Correct Answer: " + correctAnswer)             
            }

        }

        //removed some code
        return rootView;
    }
}

Now edittext2 is showing the correct answer.

String correctAnswer = currentQuestion.getAnswerText();

et2.setText("Correct Answer: " + correctAnswer) 

But the user entered valus in edittext1 is getting cleared.

String input = et1.getText().toString().trim();

et1.setText(input)

Now my question is "After finishing the test its moving to ResultsPage. In resultspage after clicking the "Review Answers" button its moving back to fragment activity.(At that time the user entered values getting cleared). How to avoid these edittext values getting cleared?"

来源:https://stackoverflow.com/questions/61995729/fragment-edittext-data-getting-cleared-when-i-am-moving-to-another-activity-and

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!