Array of sentences in Java

0

I'm studying Java, and Android development, and I want to make this application to understand how it works.

I'm using the Toast class so that when I click on the image, a phrase appears, I managed to make it work with numbers, but instead of the numbers I want to put some sentences and when I click on the image, the phrase appears for a few seconds in the screen.

How do I do this without too many changes to this code I already have?

    package android.tutorial.android;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

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

public class MainActivity extends Activity {

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
  }

// public void mostrarMensagem(View view) {
    //Toast toast = Toast.makeText(this.sorteia(), Toast.LENGTH_SHORT);
    //toast.show();

  //}


 public void mostrarMensagem(View view) {
     int[] lista = new int[]
             {
                1, 2, 3, 4, 5, 6, 7, 8, 9, 10
             };
     StringBuilder builder = new StringBuilder();
     for(int i : lista)
     {
          builder.append("" + i + " ");
     }
     Toast.makeText(this, builder, Toast.LENGTH_LONG).show();


      }


}

    
asked by anonymous 20.02.2015 / 18:43

1 answer

1

Do something like this:

public void mostrarMensagem(View view) {
    String[] lista = new String []{"sentence one", "sentence two"};
    String randomStr = lista[new Random().nextInt(lista.length)];

    Toast.makeText(this, randomStr, Toast.LENGTH_LONG).show();
}

Remembering that there is no rule of not being able to repeat the sentence, for that you would have to do something else.

    
20.02.2015 / 19:08