implementing onClick

Asked

Viewed 226 times

1

My problem is this:

I have a string array with the name phrases, where I have several sentences.

And I’d like to show them all in one TextView one by one each time you press the button "next".

Any hint will be welcomed thanks.

  • 1

    Welcome to Stack Overflow! So that the community can help you, it is important that you explain in detail your problem, and show what you have done so far! I suggest you read the articles: Tour and how to ask a question. Thank you!

1 answer

1


Hello ! Here’s an example of what your OnCLick:

int ponteiro = 0;
private View.OnClickListener nextAction(){
return  new View.OnClickListener() {
    @Override
    public void onClick(View v) {      

        // vamos ver se o ponteiro é menor que o tamanho da lista
        // tiramos 1, pois abaixo vamos adicionar mais 1!
        if(ponteiro < (frases.size()-1) ){
        // vamos andar uma posição com o ponteiro
        ponteiro++;
        }else{ 
       // vamos zerar o ponteiro
        ponteiro = 0;
       }
         // Agora vamos mostrar o item da Lista
         meuTextView.setText(frases.get(ponteiro));
};
}

To add the action to your button :

botaoProximo.setOnClickListener(nextAction());
  • speaks Thiago Luiz Domacosk, thanks for trying to help guy, so. declared my array like this: String [] sentences = getResources(). getStringArray (R.array.phrases); however when I try to use the combination.size phrases in the iteration if "size " does not exist...can you tell me why?

  • Try length in place of size(). The estate length refers to the String[], and the method size() and of a List<String>

  • And instead of get phrases(pointer) use [pointer]

  • Vlw Thiago Luiz Domacoski. It worked. Thank you.

  • Thiago tried to implement the "previous" button and I did so within the method: 'if (pointer<phrases.langth){ pointer-;} Else {what do I put here? }' I’ve tried several ways but I couldn’t help myself?

  • If next we go to the top of the list, in the previous, we go to the end! Then put: pointer=sentences.length!

  • The if is different in the case of the previous one: must compare if pointer is greater than zero: if(pointer<0){ pointer-; }Else{pointer =phrases.length;}

  • Good your logic...I did as you explained but when you arrive at position 0, instead of going to the end of the array the app hangs...where will be the error?

  • Sorry, try as follows: if(pointer>0) While greater zero goes down, or it goes back to the end

  • Not giving the error now happens in passing 1 to 0 the app hangs

  • What error it shows?

  • Try: pointer= phrases.length-1;

  • Now it worked Thiago, all that was missing was "sentences.length-1

  • Thanks Thiago

Show 9 more comments

Browser other questions tagged

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