0

In an android app I intend to allow users to answer a random sum then a new one appears on screen. This is repeated 10 times and then a final score will then be given. However I am unsure how to update the sum so that after each each a new random is shown on screen.

Below is my current code:

public class Test extends Activity {
    //declare vars
    TextView text;
    EditText answer;
    Button submit;
    int random1;
    int random2;
    String question;
    int correctAnswer;@
    Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.test);
        // initialising variables
        initialiseVars();
        //set up random
        setUpRandom();
        //Set text view equal to question
        text.setText(question);
        //updateQuestion?
    }
    public void initialiseVars() {
        text = (TextView) findViewById(R.id.tvTopRandomTest);
        answer = (EditText) findViewById(R.id.etEnterAnswerRandomTest);
        submit = (Button) findViewById(R.id.btnSubmitRandomTest);
    }
    public void setUpRandom() {
        //setting up randoms
        Random random = new Random();
        // Generating random number between 1 and 12
        random1 = random.nextInt(12) + 1;
        // Generating another random number between 1 and 12
        random2 = random.nextInt(12) + 1;
        question = random1 + " x " + random2 + " = ";
        correctAnswer = random1 * random2;
    }
    public void updateQuestion() {
        //CODE TO UPDATE QUESTION
    }
}
Ankit Popli
  • 2,809
  • 3
  • 37
  • 61
  • 1
    what is the question ? – Shayan Pourvatan Apr 05 '14 at 11:42
  • You want to change numbers every time when user clicks 'submit'? – Alex Salauyou Apr 05 '14 at 11:43
  • Each time the user enters their answer for the sum, I want the textview to refresh with another random sum for them to answer? –  Apr 05 '14 at 11:44
  • Are you going to take the sum of the users question and use that to generate a new sum or what ? – Lasse Apr 05 '14 at 11:44
  • No I am saving their answer and going to show if its correct or not in the next activity. I already have the code to do this. I am just unsure of the code to update the textview to a new sum after each answer has been submitted. Thanks! –  Apr 05 '14 at 11:48
  • if you want to set the textview content do it like this: `text.setText(YourSum);` – Lasse Apr 05 '14 at 11:49
  • yes but will this create a new random after each click? –  Apr 05 '14 at 11:51
  • The code will get reruned,so yes it will. – Lasse Apr 05 '14 at 11:57

1 Answers1

2

Add Button ClickListener so that when user press submit button it will update question and clean all previous values

submit = (Button) findViewById(R.id.btnSubmitRandomTest);
submit.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        updateQuestion();
    }
}

Maintain a count in your activity and increase it in updateQuestion

public void updateQuestion() {
    if (Int.parseString(answer.getText().toString()) != correctAnswer) {
        // Show toast or something
        return;
    }
    tries++;
    if (tries == 10) return; // or do something else;
    answer.setText("");
    setUpRandom();
    text.setText(question); // add this line in your setUpRandom();
}

To generate random integers look at this. Hopefully this will help you out.

Community
  • 1
  • 1
Faisal Ali
  • 1,135
  • 10
  • 17
  • 1
    That is the way to do it !^-^ – Lasse Apr 05 '14 at 11:58
  • Thank you, if I wanted to show the questions that the user answered and the answers they gave in the question activity, would I have to pass both an array of the questions and an array of their answers through to the next activity via an intent? –  Apr 05 '14 at 12:04
  • Exactly you need to maintain Question and Answer's List(Or create a class containing both question and answer but it needs to be implemented from Parcelable Class) and pass it via Intent. – Faisal Ali Apr 05 '14 at 12:09
  • For your help http://stackoverflow.com/questions/5819238/help-with-passing-arraylist-and-parcelable-activity – Faisal Ali Apr 05 '14 at 12:10