简体   繁体   中英

How should I save string from the Radio Button in arraylist and display it on next page in text box

I am working on Quiz app in which there is a question activity which contains 10 questions. Now the problem is I want to get the selected text on radio button in array and display it on next page in textbox where I am already displaying the correct answer and the question from database

here is the code from first page of radio buttons

List<String> ls = new ArrayList<String>();
    private boolean checkAnswer() {
    String answer = getSelectedAnswer();
    if (answer == null) {
        return false;
    } else {
        ls.add(answer);
        if (currentQ.getAnswer().equalsIgnoreCase(answer)) {
            currentGame.incrementRightAnswers();
        } else {
            currentGame.incrementWrongAnswers();
        }
    return true;
    }
}

/**
 * 
 */
public String getSelectedAnswer() {

    if (c1.isChecked()) {
        return c1.getText().toString();

    }
    if (c2.isChecked()) {
        return c2.getText().toString();
    }
    if (c3.isChecked()) {
        return c3.getText().toString();
    }
    if (c4.isChecked()) {
        return c4.getText().toString();
    }

    return null;
}

@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
    // TODO Auto-generated method stub
    // Log.d("Questions", "Moving to next question");
    a++;
    qno++;

    if (!checkAnswer())
        return;

    if (currentGame.isGameOver()) {
        // stoptime();
        db.open();
        String total = currentGame.getRight() + "";
        db.insertOptions(topic1, total, mon);
        db.close();
        alertb();
        // mCountDown.cancel();
        a = 0;
        qno = 1;

    } else {
        Intent i = new Intent(this, QuestionActivity.class);

        startActivity(i);
        finish();
    }
}

public String show(int i) {
    String a = ls.get(i);
    return a;
}

In my checkanswer() I am adding the text of radio button in list ls.add(Item). but here I am making some mistake please correct me

I have written show() on above page which would help me to get elements of arraylist on next page here is my code on next page

public static String getAnswers(List<Question> questions) {
        int question = 1;
        StringBuffer sb = new StringBuffer();


        for (Question q : questions){


            sb.append("Q").append(question).append(") ").append(q.getQuestion()).append("? \n");
            sb.append("Your Answer: ").append(qa.show(question)).append("\n");
            sb.append("Correct Answer: ").append(q.getAnswer()).append("\n");

            sb.append("____________________________________________________________________________________").append("\n\n");
            question ++;
        }

From this page my correct answer and Questions are being displayed but the answer which I am selecting is not working

It gives me error as"array index out of bound no element at position 1" please help me

Data members loose all values when the activity class gets killed in android. Hence, you are getting IOOBE while accessing the list values declared in your previous activity. Create a bundle and add the following code in your previous activity's onDestroy method

Bundle b = new Bundle();
b.putStringArrayList("answers", ls);

set this bundle to the intent that you use to start new activity

Intent i = new Intent(this, QuestionActivity.class);
i.setExtra(b)

In your other activity's onCreate function use the following code :

Bundle bundle = getExtras();
List<String> tmp = bundle.getStringArrayList("answers")

Now you will get all the data that was loaded into last activity's list variable.

If you have mapping in between your questions and answers list then loop with a temp variable and use it as index to your lists to get corresponding mapped data members. Also, start from 0.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM