0

For my "memory" game, I'm using a GridView to display 12 cards.
At the beginning, the cards are all the same.

In all the examples I found, you see something similar to this code to add the picture ids to your ImageAdapter:

 private Integer[] mThumbIds = {     
        R.drawable.card_back, R.drawable.card_back,
        R.drawable.card_back, R.drawable.card_back,
        R.drawable.card_back, R.drawable.card_back,
        R.drawable.card_back, R.drawable.card_back,
        R.drawable.card_back, R.drawable.card_back,
        R.drawable.card_back, R.drawable.card_back
    };

Is there a shorter,cleaner, nicer way? something like this:

private Integer[] mThumbIds = new Integer[11];
Arrays.fill(mThumbIds, R.drawable.card_back);

full class:

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import java.util.Arrays;

public class ImageAdapter extends BaseAdapter {
    private Context mContext;

    public ImageAdapter(Context c) {
        mContext = c;
    }

    public int getCount() {
        return mThumbIds.length;
    }

    public Object getItem(int position) {
        return null;
    }

    public long getItemId(int position) {
        return 0;
    }

    // create a new ImageView for each item referenced by the Adapter
    public View getView(int position, View convertView, ViewGroup parent) {
        ImageView imageView;
        if (convertView == null) {
            // if it's not recycled, initialize some attributes
            imageView = new ImageView(mContext);
            imageView.setLayoutParams(new GridView.LayoutParams(150, 150));
            imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
            imageView.setPadding(8, 8, 8, 8);
        } else {
            imageView = (ImageView) convertView;
        }

        imageView.setImageResource(mThumbIds[position]);
        return imageView;
    }

    // references to our images

    Integer[] mThumbIds = new Integer[11];
    Arrays.fill(mThumbIds, R.drawable.card_back);


       /*   R.drawable.card_back, R.drawable.card_back,
            R.drawable.card_back, R.drawable.card_back,
            R.drawable.card_back, R.drawable.card_back,
            R.drawable.card_back, R.drawable.card_back,
            R.drawable.card_back, R.drawable.card_back,
            R.drawable.card_back, R.drawable.card_back
    };*/

}
1
  • 1
    Did you try that code? Commented Jun 13, 2015 at 7:53

3 Answers 3

2

Actually, that's exactly it - you can use java.util.Arrays#fill:

Arrays.fill(mThumbIds, R.drawable.card_back);
Sign up to request clarification or add additional context in comments.

3 Comments

ok, but im getting these errors: Syntax error on token "(", < expected Syntax error, insert ">" to complete Reference Syntax error, insert "Identifier (" to complete MethodHeaderName
@Klaus please post your full code and the exact error in the question. Otherwise, we can't help you.
Arrays.fill is not a valid statement. You can't use in the class scope like you were declaring a member. Put that line of code in your constructor
0

You can use a for loop to initialize your array:

int arr[] = new int[12]; // 0 ... 11
for(int i = 0; i < arr.length; i++)
{
    arr[i] = R.drawable.card_back;
}

Comments

-1

Sorry, to low rep to commment @Mureinik answer. Usually to keep my constructors clean and initialize variables that are always the same for all instances i do it this way:

private Integer[] mThumbIds = new Integer[11];
{
    Arrays.fill(mThumbIds, R.drawable.card_back);
}

This block gets called per instance before the constructor.

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.