How to make a TextView display the contents of an Array if I'm running it with a For

1

I have an Array of int that stores the Questions in (ID) mode for this reason it is not of type String. I have another Boolean Array that stores the correct answers.

  

The original idea is that a for cycle traverses the Array of questions and   the counter shows what is in the certain positions by   After the TextView the user should press the button   true / false to give the value to a variable that will be compared with   "Correct answer" so that in the end a Toast will return   correct or incorrect as the condition is met with an if / else   simple.

I DO NOT ACHIEVE THE TEXVIEW SHOW WHAT IS IN THE "n" POSITION OF THE STICKER ARRAY. HOW CAN I MAKE THE TEXVIEW SHOW EACH OF THE POSITIONS.

QuizActivity.java

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;


public class QuizActivity extends Activity {

    private Button mTrueButton;
    private Button mFalseButton;
    private int mostradorPregunta;


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


         //= findViewById(0x7f060029);

        //COMIENZA EL ARRAY DE PREGUNTAS

        int preguntas [] = new int[3];
        preguntas [0] = 0x7f060025;
        preguntas [1] = 0x7f060026;
        preguntas [2] = 0x7f060027;
        preguntas [3] = 0x7f060028;


        //COMIENZA ARRAY DE RESPUESTAS

        boolean respuestaCorrecta [] = new boolean[3];
        respuestaCorrecta [0] = true;
        respuestaCorrecta [1] = false;
        respuestaCorrecta [2] = false;
        respuestaCorrecta [3] = true;

        TextView mVisorPregunta = new TextView(this);
        mVisorPregunta.setText(mVisorPregunta);

        for (mostradorPregunta = 0; mostradorPregunta < preguntas.length; mostradorPregunta++){
            mVisorPregunta = mostradorPregunta;
        }




        mTrueButton = findViewById(0x7f0b005e);
        mTrueButton.setOnClickListener(new View.OnClickListener(){
          @Override
                  public void onClick (View v){
              Toast.makeText(QuizActivity.this, R.string.correct_toast, Toast.LENGTH_SHORT).show();
          }
            });



        mFalseButton = findViewById(0x7f0b005f);
        mFalseButton.setOnClickListener(new View.OnClickListener(){
        @Override
                public void onClick (View v){
            Toast.makeText(QuizActivity.this, R.string.incorrect_toast, Toast.LENGTH_SHORT).show();

        }
        });

    }

strings.xml

<resources>
    <string name="app_name">GeoQuiz</string>
    <string name="question_text">¿LOS GATOS TIENEN DOS OJOS?</string>
    <string name="true_button">Cierto</string>
    <string name="false_button">Falso</string>
    <string name="correct_toast">Correcto!</string>
    <string name="incorrect_toast">Incorrecto!</string>

    <string name="pregunta1">¿El cielo es azul?</string>
    <string name="pregunta2">¿Android es desarrollado por Apple?</string>
    <string name="pregunta3">¿Los gatos pueden respirar bajo el agua?</string>
    <string name="pregunta4">¿Las aguilas pueden volar?</string>
</resources>

activity_quiz.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:orientation="vertical" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="24dp"
        android:text="@string/question_text"/>

   <LinearLayout
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:orientation="horizontal">

       <Button
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:text="@string/true_button"
           android:id="@+id/true_button"/>
       <Button
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:text="@string/false_button"
           android:id="@+id/false_button"/>

   </LinearLayout>






</LinearLayout>

    
asked by Alexis Olveres 04.08.2017 в 17:12
source

2 answers

0

This:

 int preguntas [] = new int[3];
    preguntas [0] = 0x7f060025;
    preguntas [1] = 0x7f060026;
    preguntas [2] = 0x7f060027;
    preguntas [3] = 0x7f060028;

You should be marking a java.lang.ArrayIndexOutOfBoundsException because you're telling him that your arrangement is 3 positions and you're getting 4.

Change this line:

int preguntas [] = new int[3];

For this:

int preguntas [] = new int[4];
    
answered by 04.08.2017 в 17:20
0

What I would do, would be the following:

ContadorPreguntas=0;
mTrueButton = findViewById(0x7f0b005e);
        mTrueButton.setOnClickListener(new View.OnClickListener(){
          @Override
                  public void onClick (View v){
              Toast.makeText(QuizActivity.this, R.string.correct_toast, Toast.LENGTH_SHORT).show();
              if(ContadorPreguntas<preguntas.lenght()){
              ContadorPreguntas++;
              mVisorPregunta.setText(preguntas[ContadorPreguntas]);
              }

          }
            });

Logically, I would do the same with the false button. I explain, I added a counter called ContadorPreguntas that will serve to know the index of the array which should find the answer, start at 0. before modifying the value of the counter, verify that this is not greater than the size of the array (for avoid ArrayIndexOutOfBoundsException ), if the counter is smaller than the size of the array, then add 1 to the counter and update the TextView with the property setText in which I pass the array questions with index ContadorPreguntas

EDIT the following code is incorrect:

TextView mVisorPregunta = new TextView(this);

on android, you must cast the XML element:

TextView mVisorPregunta=(TextView)findViewById(R.id.algo) //"algo" es el ID que tiene mVisorPregunta en tu XML
    
answered by 04.08.2017 в 17:23