0

I'm making my first android app and I want to to make a test app, I'm using a variable and "If"/"Else if" statements in order to change the text of the question (TextView) and answers (RadioButtons). The problem is that when the user presses the button for the first time the answers and question change but when the user press the button for the second time the question and answers don't change and I don't know why...

Here is my activity:

package com.example.test;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

public class Test extends Activity {

TextView titulo;
RadioGroup buttons;
RadioButton opcn1;
RadioButton opcn2;
RadioButton opcn3;
RadioButton opcn4;

int pregunta = 0;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.memetest);

    titulo = (TextView) findViewById(R.id.textView1);

    buttons = (RadioGroup) findViewById(R.id.radioGroup1);

    opcn1 = (RadioButton) findViewById(R.id.radio0);
    opcn2 = (RadioButton) findViewById(R.id.radio1);
    opcn3 = (RadioButton) findViewById(R.id.radio2);
    opcn4 = (RadioButton) findViewById(R.id.radio3);


    Button siguiente = (Button) findViewById(R.id.siguiente);
    siguiente.setOnClickListener(new OnClickListener() {


        @Override
        public void onClick(View v) {

            pregunta = + 1;

            if (pregunta == 1){ 

                titulo.setText(getString(R.string.Q2));
                opcn1.setText(getString(R.string.Q2O1));
                opcn2.setText(getString(R.string.Q2O2));
                opcn3.setText(getString(R.string.Q2O3));
                opcn4.setText(getString(R.string.Q2O4));

            }
            else if (pregunta == 2){

                titulo.setText(getString(R.string.Q3));
                opcn1.setText(getString(R.string.Q3O1));
                opcn2.setText(getString(R.string.Q3O2));
                opcn3.setText(getString(R.string.Q3O3));
                opcn4.setText(getString(R.string.Q3O4));

            }
        };

    });
}
}

I tried using "break" but the problem persists.

Help, please.

William Pownall
  • 760
  • 7
  • 9
Jacobo
  • 87
  • 1
  • 1
  • 13

1 Answers1

3

To add 1 to your variable on each click event, try using this instead:

pregunta++;

(Understand this is the same as pregunta += 1; or pregunta = pregunta + 1;)


What you wrote pregunta = + 1; means pregunta = +1; which is nothing more than

pregunta = 1;
Sam
  • 86,580
  • 20
  • 181
  • 179
  • Thanks a lot! I'm still learning and I forgot that "pregunta = + 1;" will always set "1" to the variable "pregunta" instead of add "1" to it. Now my Test work fine =) Thanks! – Jacobo Oct 02 '12 at 22:53