0

Here is some code:

public class Article {
(...)
private static int[] sImages =  { R.drawable.ic_picture};
private ImageView mImage;
public Article(int a)
{
    mImage = sImages[a];
}

In: mImage = sImages[a]; There is this error:

Incompatible types
Required: android.wiget.ImageView
Found: Int

How to convert int to android.widget.ImageView or something like this?

(I create RecyclerView and in that is a lot of ImageView and i want to have List with Images to these ImageViews.)

Hubert Hubert
  • 77
  • 3
  • 14

3 Answers3

2

You can use setImageResource. When you call ur Article class, also pass the view.

private static int[] sImages =  { R.drawable.ic_picture};
private ImageView mImage;
public Article(int a,  View view)
{
    mImage = (ImageView) view.findViewById(R.id.image);
    mImage.setImageResource(sImages[a]);
}
Shank
  • 1,387
  • 11
  • 32
0

If you have a list you have to write a RecyclerAdapter class to hold and set items of list.

see this: Using lists and grids in Android with RecylerView - Tutorial

FarshidABZ
  • 3,860
  • 4
  • 32
  • 63
0

Your code block shows an Article class that is supposed to be attached to the RecyclerView with a corresponding image, is what I assume from your question. In your comments you have

for (int i = 0; i < 30; ++i) { articles.add(new Article(i))}

  1. You set 30 as a hard coded limit instead you should set it to the size of your image id array.
  2. You're aware that you're incrementing your index before you add the article which will cause you to lose out on the id at index 0.
  3. All this does is pass an int through your constructor to grab the id at that array index in your Article class you should not have a static array of images in an object class.

Change your Article class to look like this:

public class Article {
    ...
    int imageID;
    public Article(int imageID) {
        this.imageID = imageID;
    }
    public int getImageID() {
        return imageID;
    }
   ...
}

Now when you loop through and add your articles it should like like this:

for (int i = 0; i < imageIDArray.length; i++) {
    articles.add(new Article(imageIDArray[i]);
}

Now you're setting the imageID to be whatever value is in your imageIDArray once you're in the onBindViewHolder() method of your RecyclerView you can then easily set the correct image by using

setImageDrawable(ContextCompat.getDrawable(context, article(i).getImageID));

Pztar
  • 4,274
  • 6
  • 33
  • 39