Note that there are some explanatory texts on larger screens.

plurals
  1. POUsing loop index in an Android OnClickListener anonymous class to create a grid of ImageButtons
    primarykey
    data
    text
    <p>I am creating an Android application for a card game. Basically, I have it so there are four rows of three card on the screen at a time, each card being represented by a pressable <code>ImageButton</code>. My problem is that I want to use a for loop so I can create <code>OnClickListeners</code> for these twelve cards, while having a way to keep track of what number card we are on so I can properly keep track of which button is pushed. In the code below, I use counter to keep track of how many cards are pushed, and <code>pressed_index</code> to keep track of the three cards that are pushed. If counter doesn't equal three, I change the color filter on the <code>ImageButton</code> to make it appear pushed. However, when I set anonymous class, I cannot use the x variable because it is not allowed in the anonymous class. I need to be able to set the <code>pressed_index</code> to the value of the current card, otherwise I'll have no way of knowing which of the 12 cards is being pushed. </p> <pre><code>ImageButton[] hand = new ImageButton[12]; int[] pressed_index = new int[3]; int counter = 0; for (int x = 0; x &lt; 12; x++) { hand[x] = (ImageButton)findViewById(R.id.//card_1, card_2, etc.); hand[x].setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { pressed_index[counter] = x; counter++; if (counter != 3) { ((ImageView) v).setColorFilter(Color.argb(150, 155, 155, 155)); } } }); } </code></pre> <p>The alternative, which is how I have it now and the only way I can think to do it otherwise is to hard-code all twelve methods and physically putting in each number. This is very inefficient though, and something that makes it very hard to update the code. The problem of setting the ID correctly isn't bad, I simply get the ID from a string constructed from the x, like so...</p> <pre><code>int ID = getResources().getIdentifier(("card_"+(x+1)),"id","com.example.project"); hand[x] = (ImageButton)findViewById(ID); </code></pre> <p>...similar to the solution found here: <a href="https://stackoverflow.com/questions/4865244/android-using-findviewbyid-with-a-string-in-a-loop">Android: Using findViewById() with a string / in a loop</a> However, the problem of using the x is unsolvable by me up to this point, even after much research. Through posts like <a href="https://stackoverflow.com/questions/13147120/accessing-variables-from-onclicklistener">Accessing variables from onclicklistener</a> and <a href="https://stackoverflow.com/questions/9117164/android-accessing-a-global-variable-inside-an-onclicklistener">Android: Accessing a global Variable inside an onClickListener</a>, it seems only <code>final</code> variables can be passed, and one common fix is to create a separate class instead of using an anonymous class. This, however, didn't lead me very far, as when I began writing the separate class for the <code>OnClickListener</code>, I ran into the same problem, as I couldn't pass the variable to create the <code>OnClickListener</code> to match it up to the correct card. Please let me know if you have any ideas as to how I can fix this.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload