尝试使用ArrayList来保存图像资源
在我的应用程序中,我的可绘制文件夹中有一堆图像,我随机选择这些图像并使用 imageView 显示。我听说 ArrayList 可以从列表中添加/删除对象...为了防止图像重复,我在下面使用了一些示例代码:
// 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。任何线索如何解决这个问题?
提前致谢!
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 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??
Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用
List; imageHolder = new ArrayList();
Use
List<Integer> imageHolder = new ArrayList<Integer>();
ArrayList 始终包含对象,从不包含原始类型。当您将int
设置到其中时,它们会自动装箱为Integer
对象,当您取回它们时,您也会获得 Integer 对象。一个简短的修复是:不过要小心,拆箱空指针将导致 NullPointerException,因此请确保您的 randInt 在 arraylist 的范围内。
编辑:
我完全错过了这一点,但是你像这样初始化你的ArrayList
:它创建了对象的ArrayList。相反,像下面这样初始化 ArrayList 以创建整数 ArrayList:
ArrayList contains Objects, always, never primitive types. When you setint
s into it, they are autoboxed toInteger
objects, when you get them back, you get Integer objects as well. A short fix will be:Be careful though, unboxing a null pointer will cause a NullPointerException, So make sure your randInt is in the range of the arraylist.
EDIT:
I totally missed that, butYou initialize yourArrayList
like that:Which creates ArrayList of Objects. instead, initialize the ArrayList like the following to create ArrayList of integers: