0

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!

2 Answers 2

1

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];
    ...
}
Sign up to request clarification or add additional context in comments.

1 Comment

That was easy :) It's working perfectly now, thank you so much!
0

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);
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.