[英]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. 您的代码块显示了应该与相应图像一起附加到
RecyclerView
的Article
类,这是我从您的问题中得出的假设。 In your comments you have 在您的评论中
for (int i = 0; i < 30; ++i) { articles.add(new Article(i))}
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 现在,一旦您进入
RecyclerView
的onBindViewHolder()
方法,就可以将imageID
设置为imageIDArray
任何值,然后可以通过使用轻松地设置正确的图像
setImageDrawable(ContextCompat.getDrawable(context, article(i).getImageID));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.