尝试使用ArrayList来保存图像资源 [英] trying to use ArrayList to hold image resources

查看:638
本文介绍了尝试使用ArrayList来保存图像资源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序,我在我的文件夹绘制了一堆图片,我随意和显示用ImageView的选择。有人告诉我有关的ArrayList的,可以从列表中的顺序添加/删除对象... 以prevent图像重复后,一些样本code下面我用:

In my app, I have a bunch of images in my drawable folder which I select at random and display using imageView. I've been told about ArrayList's which can add/remove objects from the list...in order to prevent image repeats, some sample code I used below:

// create an array list 
ArrayList imageHolder = new ArrayList(); 
int remaining = 10;

public void initArrayList(){
    // add elements to the array list 
    imageHolder.add((int)R.drawable.child0); 
    imageHolder.add((int)R.drawable.child1); 
    imageHolder.add((int)R.drawable.child2); 
    imageHolder.add((int)R.drawable.child3); 
    imageHolder.add((int)R.drawable.child4); 
    imageHolder.add((int)R.drawable.child5); 
    imageHolder.add((int)R.drawable.child6); 
    imageHolder.add((int)R.drawable.child7); 
    imageHolder.add((int)R.drawable.child8);
    imageHolder.add((int)R.drawable.child9); 
}

//get random number within the current range
int randInt = new Random().nextInt((remaining-1));

//update the imageView config    
ImageView image = (ImageView) findViewById(R.id.shuffleImageView);
image.setImageResource(imageHolder.get(randInt));

Eclipse的报告说,image.setImageResource不能使用对象的说法,这是由ArrayList中提供。实际的参数应该是int。任何线索如何解决这个问题?

Eclipse reports that image.setImageResource cannot use an object argument, which is what is provided by arrayList. The actual argument should be int. Any clue how to get around this??

在此先感谢!

推荐答案

使用列表<整数GT; imageHolder的=新的ArrayList<整数GT;();

这篇关于尝试使用ArrayList来保存图像资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆