Doubt how to jump 7 in 7 in the ids of own onItemClickListener android

Asked

Viewed 50 times

1

How to make a condition in which it will jump 7 by 7 because I’m having to set it manually in hand, and if you have data on the defined ids, it will give error there would complicate . Below is an example of the code to which I need to decrease and make it always add 7 ,as long as there are items in listview:

if (id_item_titulo == 0 ||id_item_titulo == 7 || id_item_titulo == 14 || id_item_titulo == 21 || id_item_titulo == 28 || id_item_titulo == 35
                    || id_item_titulo == 42 || id_item_titulo == 49 || id_item_titulo == 56 || id_item_titulo == 63
                    || id_item_titulo == 70 || id_item_titulo == 77 || id_item_titulo == 85 || id_item_titulo == 92
                    || id_item_titulo == 99 || id_item_titulo == 106 || id_item_titulo == 111 || id_item_titulo == 118
                    || id_item_titulo == 125 || id_item_titulo == 132) {

}

1 answer

6


Sounds like a case for the operator of rest %:

// Se o ID for múltiplo de 7...
// Em outras palavras, se o resto da divisão por 7 for zero...
if(id_item_titulo % 7 == 0) {
    // ...
}
  • In case it then goes to 14,21,28 and so goes 7 in 7 automatically ? I want to avoid setting the values of the IDS clicked.

  • He won’t "go" anywhere, but he’ll only get into if if the ID is multiple of 7, or zero. Example: http://ideone.com/OkRJvV

  • 1

    I was intrigued by your question above, and I thought knowing this might help you: id_item_titulo % 7 is a expression where % is the operator, and what’s around him are the operandos. This expression returns a value (the rest of the division by 7), which is then compared to zero.

  • Friend, arriving home I will try this idea, and I come back here to inform you whether I got it or not, I thank you already in advance.

  • Thanks, I put if (id_do_item % 2 == 0 || id_do_item % 2== 1 ) { //**** } and it worked : )

  • But @Diegokappaun, that you put does not jump 7 in 7!

  • The 7-in-7 was to do a single action of a particular item, only I put that new condition to take in all and decrease the size of the code. I had put a condition in the other class of when clicking on the blank space it did not open an Activity, while the others could normally, and already in the class that set this new condition, they could be even and odd, so I had no problem adding and updating .

  • But... but... if (id_do_item % 2 == 0 || id_do_item % 2== 1 ) makes no sense! Any integer will always be even or odd. So you don’t even need the if. @Diegokappaun

  • I did tests and when I put multiple methods inside the IF with only the different condition, it runs only the item that of the rest 0 and I need it to be 0 and 1 for the other items to be changed smoothly, is that my way of creating the program was a little different .

Show 4 more comments

Browser other questions tagged

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