Problem when initializing an array in AndroidStudio

1

I'm making an application that every time you press a button, change the text that is displayed by a random string. The strings are stored in an arraylist. The code I had so far was the following:

private TextView texto;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_basic_game);


    texto = (TextView) findViewById(R.id.textView);
}

public void cambiarMensaje(View v){

    int aleatorio;
    int total;

    ArrayList<String> preguntas = new ArrayList<>();


    preguntas.add("String 1");
    preguntas.add("String 2");
    ...

    int longitud = preguntas.size();
    while(longitud > 0){
        aleatorio = (int) (Math.random() * longitud);
        String s = preguntas.get(aleatorio);
        texto.setText(String.format(s));
        preguntas.remove(aleatorio);
        longitud--;
    }
}

The problem is that every time you pressed the button, the arraylist was initialized with all the phrases again. I tried to solve it with the following changes:

private TextView texto;
ArrayList<String> preguntas = new ArrayList<>();

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

    preguntas.add("String 1");
    preguntas.add("String 2");
    ...

    texto = (TextView) findViewById(R.id.textView);
}

public void cambiarMensaje(View v){

    int aleatorio;
    int total;





    int longitud = preguntas.size();
    while(longitud > 0){
        aleatorio = (int) (Math.random() * longitud);
        String s = preguntas.get(aleatorio);
        texto.setText(String.format(s));
        preguntas.remove(aleatorio);
        longitud--;
    }
}

The problem now is that when I press the button a single phrase is shown (the button works only once). So my question is that I do not quite understand where I'm supposed to add the phrases to the arraylist and where I have to declare it, or if there is some way to fix the second code.

    
asked by Petergente 23.11.2018 в 16:16
source

2 answers

0

I would use an auxiliary array that would be filled at the moment of pressing the button, with the same values of the question array. And then, within the while you'll work with your new array, so that "questions" will never change. Something like this:

public void cambiarMensaje(View v){
    ArrayList<String> auxiliar = new ArrayList<>();
    int aleatorio;
    int total;

    for (int i = 0; i < preguntas.size(); i++) {
        String valor = preguntas[i];
        auxiliar.add(valor);
    }
    int longitud = preguntas.size();

    while(longitud > 0){
        aleatorio = (int) (Math.random() * longitud);
        String s = auxiliar.get(aleatorio);
        texto.setText(String.format(s));
        auxiliar.remove(aleatorio);
        longitud--;
    }
}

For the rest of the code you will keep the second option that you uploaded.

    
answered by 23.11.2018 в 16:40
0

It's not really an initialization problem, what happens is that you're removing the elements until you leave only one in the ArrayList .

According to your loop, while the length of ArrayList is greater than 0 you will get an element randomly from your ArrayList but will eliminate that element ( preguntas.remove(aleatorio) ), therefore in the end you would only get one element, but I eliminate all others in ArrayList .

If you want to keep the elements in ArrayList avoid using remove() :

 while(longitud > 0){
        aleatorio = (int) (Math.random() * longitud);
        String s = preguntas.get(aleatorio);
        texto.setText(String.format(s));
       // preguntas.remove(aleatorio);
        longitud--;
    }

This will keep the elements you initially added in the ArrayList .

I see you add the message to TextView or EditText

    texto.setText(String.format(s));

If you want to get a random message from the ones stored in the ArrayList you can get it in this way avoiding using a loop:

 private Random randomGenerator;

   public void cambiarMensaje(View v){

        randomGenerator = new Random();
        int index = randomGenerator.nextInt(preguntas.size());
        String s = preguntas.get(index);
        texto.setText(String.format(s));

   }
    
answered by 23.11.2018 в 16:38