简体   繁体   English

将Int转换为ImageView

[英]Convert Int to ImageView

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]; 在: mImage = sImages[a]; There is this error: 有此错误:

Incompatible types 不兼容的类型
Required: android.wiget.ImageView 必需:android.wiget.ImageView
Found: Int 找到:整数

How to convert int to android.widget.ImageView or something like this? 如何将int转换为android.widget.ImageView或类似的东西?

(I create RecyclerView and in that is a lot of ImageView and i want to have List with Images to these ImageViews.) (我创建了RecyclerView,并且其中包含很多ImageView,并且我希望这些ImageView包含带有图像的列表。)

You can use setImageResource . 您可以使用setImageResource When you call ur Article class, also pass the view. 当您调用您的Article类时,也要传递视图。

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

If you have a list you have to write a RecyclerAdapter class to hold and set items of list. 如果有列表,则必须编写一个RecyclerAdapter类来保存和设置列表项。

see this: Using lists and grids in Android with RecylerView - Tutorial 看到这个: 在Android中将列表和网格与RecylerView一起使用-教程

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. 您的代码块显示了应该与相应图像一起附加到RecyclerViewArticle类,这是我从您的问题中得出的假设。 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. 您将30设置为硬编码限制,而应将其设置为图像ID数组的大小。
  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. 您知道在添加文章之前要增加索引,这会使您迷失索引0处的ID。
  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. 这一切都是通过构造函数传递一个int来获取Article class中该数组索引处的id,而您在对象类中不应具有static图像数组。

Change your Article class to look like this: 更改您的Article类,使其看起来像这样:

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 现在,一旦您进入RecyclerViewonBindViewHolder()方法,就可以将imageID设置为imageIDArray任何值,然后可以通过使用轻松地设置正确的图像

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM