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;
        }
Rohit Kumar
  • 728
  • 9
  • 13

3 Answers3

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.

izakos
  • 265
  • 2
  • 6
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();
Hoque MD Zahidul
  • 10,560
  • 2
  • 37
  • 40
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

alex
  • 8,904
  • 6
  • 49
  • 75