amirtc
amirtc

Reputation: 3

Android - passing array through onClick

As part of my attempt to create a memory game, I have placed 12 image buttons on my layout with id names imageButton1...imageButton12. I wrote an algrorithm to create an array called cards[12] with random values to represent which card (card1..card6) is behind each imageButton, so for example if cards[5]=4 then the card behind imageButton6 is card4. Now, i'm trying to tell the program to show the appropraite card when clicking the imageButton using the array. I'm very new to android studio and as I understood I first need to use OnClickListener on all the buttons, so I done it using a loop. This is my code so far:

public class Memory extends AppCompatActivity implements OnClickListener{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_memory);

        int i;
        int[] cards = new int[12];
        // Algorithm here
        for(i=1;i<=12;i++) {
            String name = "imageButton" + i;
            int resID = getResources().getIdentifier(name, "id", "com.amir.supermemory");
            ImageButton btn = (ImageButton) findViewById(resID);
            btn.setOnClickListener(this);
        }

Now comes the onClick function, which performs the action of switching the appropraite image when clicked. The problem is I can't manage to pass the array cards[] which I created before into the function (it says "cannot resolve symbol 'cards'"), how do I do that?

public void onClick(View v) {
            switch (v.getId()) {
                case R.id.imageButton1:
                    ImageButton b = (ImageButton) findViewById(R.id.imageButton1);
                    String name = "card" + cards[0]; //cards is shown in red
                    int resID = getResources().getIdentifier(name, "drawable", "com.amir.supermemory");
                    b.setImageResource(resID);
                    break;
                //copy paste forr all imageButtons
            }
        }

Any help is appreciated, thank you!

Upvotes: 0

Views: 242

Answers (2)

somia
somia

Reputation: 611

You have declared cards[] as a local variable inside the onCreate method. Please declare it outside and try.

public class Memory extends AppCompatActivity implements OnClickListener{
int[] cards = new int[12];
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_memory);

    int i;
            // Algorithm here
    for(i=1;i<=12;i++) {
        String name = "imageButton" + i;
        int resID = getResources().getIdentifier(name, "id", "com.amir.supermemory");
        ImageButton btn = (ImageButton) findViewById(resID);
        btn.setOnClickListener(this);
    }

Upvotes: 0

buczek
buczek

Reputation: 2050

Because you are declaring your cards array locally in your OnCreate() you cannot access it in another method.

All you need to do is declare your cards array globally.

public class Memory extends AppCompatActivity implements OnClickListener{

    int[] cards;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

    int i;
    cards = new int[12];
    ...
}

Upvotes: 1

Related Questions