简体   繁体   English

Android使用arraylist在ImageView上加载图像

[英]Android Load image on ImageView using arraylist

This might be a problem that I might overlook again but if you could help me solve it, I will be very thankful. 这可能是一个我可能会再次忽视的问题,但如果你能帮助我解决它,我将非常感激。 Using this code to get images from drawable and sending it to Imageview.setImageResource (in which my Imageview is inside a gridview); 使用此代码从drawable获取图像并将其发送到Imageview.setImageResource(其中我的Imageview在gridview中); everything works well 一切顺利

private   Integer[] imageIDs = {
            //R.drawable.image1,
            R.drawable.image2,
            R.drawable.image3,
            R.drawable.image4,
            R.drawable.image5,
            R.drawable.image6,
            R.drawable.Image7,
};

//Here is my another code to call imageIDs as ImageResource
ImageView iv = (ImageView)v.findViewById(R.id.grid_item_picture);
iv.setImageResource(imageIDs[position]);
//position is a default value of gridview starting from 0 

Now what I trying to accomplish is to delete that array and convert it to ArrayList simply because need these inside data to be dynamic. 现在我想要完成的是删除该数组并将其转换为ArrayList,因为需要这些内部数据是动态的。 And so I've created this new code 所以我创建了这个新代码

ArrayList<String> array_image = new ArrayList<String>();
        array_image.add("R.drawable.image6");
        array_image.add("R.drawable.image1");

and set the ImageView ImageResource to this code 并将ImageView ImageResource设置为此代码

iv.setImageResource(array_image .get(position));

I am getting an error saying: 我收到一个错误说:

The method setImageResource(int) in the type ImageView is not applicable for the arguments (Object) Can you help me fix/figure it out? 在ImageView类型中的方法setImageResource(int)不适用于参数(对象)你能帮我修复/搞清楚吗?

You are defining an ArrayList for Strings. 您正在为字符串定义ArrayList。 However you need integers as resource identifiers. 但是,您需要整数作为资源标识符。

Do it instead like this; 是这样做的;

ArrayList<Integer> array_image = new ArrayList<Integer>();
array_image.add(R.drawable.image6);
array_image.add(R.drawable.image1);

Hey @Janwel try to use this one. 嘿@Janwel尝试使用这个。 This will work most likely. 这很有可能。

List<Integer> mL = new ArrayList<Integer>();
mL.add(R.drawable.image2);

Please try with 请试试

ArrayList<int> array_image = new ArrayList<int>();
array_image.add(R.drawable.image6);
array_image.add(R.drawable.image1);

and then 接着

iv.setImageResource(array_image .get(position));

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

相关问题 如何使用 piccaso android studio 将图像加载到 ImageView src - how to load a image to ImageView src using piccaso android studio Android,ArrayList中的ImageView对象 - Android, ImageView objects in ArrayList Android Picasso不会将所有图像加载到imageview - Android Picasso does not load all image to imageview Android从URL加载图像并显示imageview - Android load image from url and show imageview 在 Android(java) 中,如何使用 URL 而不是来自互联网的图像地址在 ImageView 中加载图像? - In Android(java), How to load Image in ImageView by using URL instead of image addresses from the internet? 使用AWS S3直接将图像加载到ImageView中,而无需下载到android中的本地存储 - Load image to imageview directly using aws s3 without downloading to local storage in android 无法使用nostra13通用图像加载器android加载imageView。 - Unable to load imageView using nostra13 Universal Image loader android . 将图像加载到ImageView JavaFX - Load image to ImageView JavaFX 如何使用 Android 将 ArrayList 数据加载到 ListView? - How to load ArrayList data into ListView using Android? 如何在Android中从URL加载没有文件扩展名的图像到ImageView - How to load image without file extension from URL to ImageView in Android
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM