When you click, I want to define the phrase through the indexes

Asked

Viewed 66 times

1

As you can see in the code, when I click I’m randomly defining the phrase, but when I click, I want the phrase that I myself define through the indexes, how do I do this?

Public class MainActivity extends AppCompatActivity {

    private TextView textoNovaFrase;
    private Button botaoNovaFrase;

    private String[] frases = {
            "Se você traçar metas absurdamente altas e falhar, seu fracasso será muito melhor que o sucesso de todos",
            "O sucesso normalmente vem para quem está ocupado demais para procurar por ele",
            "Se você não está disposto a arriscar, esteja disposto a uma vida comum"
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        textoNovaFrase = (TextView) findViewById(R.id.textoNovaFraseId);
        botaoNovaFrase = (Button) findViewById(R.id.botaoNovaFraseId);

        botaoNovaFrase.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Random randomico = new Random();
                int numeroAleatorio = randomico.nextInt( frases.length );

                textoNovaFrase.setText( frases[ numeroAleatorio ] );
            }
        });
  • The question got confused for me, can explain better what you want to do?

  • When I click on the phrase botaonovafrase, it will generate a sentence, but now I do not want it to be random, I want to know which sentence I will come from, I have three sentences there, I want to put all three in order, when I click on the phrase botanovafrase, it will generate the first sentence, when I click again, will generate the second sentence, and clicking again will generate the third sentence, how to do this?

1 answer

1

If you want to place different phrases in a sequential way you can use a field in the class to know where you are going and increase as you show:

Public class MainActivity extends AppCompatActivity {
    ....
    private String[] frases = {
            "Se você traçar metas absurdamente altas e falhar, seu fracasso será muito melhor que o sucesso de todos",
            "O sucesso normalmente vem para quem está ocupado demais para procurar por ele",
            "Se você não está disposto a arriscar, esteja disposto a uma vida comum"
    };
    private int posicao = 0; //novo campo aqui

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        ...
        botaoNovaFrase.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                textoNovaFrase.setText( frases[ posicao++] ); //mostra e incrementa aqui

            }
        });

Note that this creates a problem if you click too many times because it passes the dimension of the array frases. Can improve and fix this problem by doing:

textoNovaFrase.setText( frases[posicao%frases.length] );
posicao++;

So it always generates an index between 0 a and the size of the array, which will always work correctly.

I warn you that to stay with the solution 100% correct will have to save and restore this new field in the state of Activity, particularly in onSaveInstanceState and in onRestoreInstanceState, otherwise by rotating the orientation of the device or other type of configuration changes you lose the position you were in.

Read more about the status of an Activity here

  • Thank you very much! helped me a lot, only two more questions, rs. It is possible to create conditions, like I open the Indice 2, if

  • It is possible I save the data when clicking, type I clicked once went to the index = 1, when close the application and open again, will be in the Indice = 1 and when click goes to the Indice = 2.

  • @You are welcome. Yes of course it is possible to keep. You can do this both through Sharedpreferences which is mostly used up for settings. Just as it can also do with Sqlite which is most used at the level of normal user data.

Browser other questions tagged

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