1

here is my java code i want to store all the image in drawable folder and access it through recyclerview so i created a list of array but it shows error missing ','

is getImages will return images

 public static int[] getImages() {
            int[] images =
                           {R.drawable.1,
                            R.drawable.2,
                            R.drawable.3,
                            R.drawable.4,
                            R.drawable.5,
                            R.drawable.6,
                            R.drawable.7,
                            R.drawable.8,
                            R.drawable.9,
                            R.drawable.10,
                            R.drawable.11};
            return images;
        }
2

3 Answers 3

2

The resource_name must not start with a number.

try using:

R.drawable._1 or R.drawable.img1

instead of

R.drawable.1

and rename the image accordingly.

Sign up to request clarification or add additional context in comments.

1 Comment

is it return images
2

Android generates for every resource file a constant inside R.java - class. The file name defines the name of the constant field. Field and variables names can not begin with a number:

Variable names are case-sensitive. A variable's name can be any legal identifier — an unlimited-length sequence of Unicode letters and digits, beginning with a letter, the dollar sign "$", or the underscore character "_".

From: JavaDoc Variables - Naming

Comments

2

You use a typed array in

arrays.xml

file within your /res folder that looks like this:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <array name="images">
        <item>@drawable/image1</item>
        <item>@drawable/image2</item>
        <item>@drawable/image3</item>
    </array>

</resources>

And get the array from your activity like this way :

Resources res = getResources();
TypedArray images= res.obtainTypedArray(R.array.images);
Drawable drawable = images.getDrawable(0);

OR

TypedArray images = getResources().obtainTypedArray(R.array.images);

// get resource ID by index
images .getResourceId(i, -1)

// or set you ImageView's resource to the id
mImgView1.setImageResource(images.getResourceId(i, -1));

// recycle the array
images.recycle();

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.