Problem in displaying score

Asked

Viewed 31 times

0

Hello! Well, I created an app quiz with five questions, with these, five right answers. My intention is to show on the screen how many answers the user hit and how many he missed, but I do not know a correct way to compute this data without using a thousand if Else. An XML file - from Activity questionario - has all the questions with answers and a button for other results Activity, and that’s where the ids are.

I want to show in Activity of the result something similar to this:

inserir a descrição da imagem aqui

Code of the result Activity

public class ResultadoActivity extends AppCompatActivity {

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

    /*VALOR NOME DA EDITTEXT*/
    EditText nome = findViewById(R.id.username);

    /*PEGAR SOMENTE OS IDS DAS OPTIONS CERTAS*/
    RadioButton a = findViewById(R.id.resp2);
    RadioButton b = findViewById(R.id.resp4);
    RadioButton c = findViewById(R.id.resp8);
    RadioButton d = findViewById(R.id.resp10);
    RadioButton e = findViewById(R.id.resp13);


public void callQuestao1(View view) {
    Intent intent = new Intent();
    startActivity(intent);
}

}

1 answer

1


I could not understand well what you are trying to do, but the findViewById is not for you to look for the correct answer, is for you to relate to the view that was created, for example, an editText, a button, a radio button, etc... An example:

Button botaoVerdadeiro = (Button)findViewById(R.id.btnVerdadeiro);
Button botaoFalso = (Button)findViewById(R.id.btnFalso);

You should save the answers in some variable, a variable to save the option that the user selected, one to accumulate the hits and then create a method to run when clicking the button:

botaoVerdadeiro.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    opcao = "verdadeiro";
                    if(opcao.equals(resposta)){
                        totalAcertos++;
}

You could also create a Finish button and send the data via Intent:

Intent intent = new Intent(MainActivity.this, Resultado.class);

intent.putExtra(TOTAL_ACERTOS, totalAcertos);                        

startActivity(intent);

In the Outcome Act you’d have to do something like this:

Intent intentResultado = getIntent();
int totalAcertos = intentResultado.getIntExtra(MainActivity.TOTAL_ACERTOS, 0);

TextView acertos = (TextView)findViewById(R.id.textAcertos);
acertos.setText(resultadoAcertos);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.