如何编写一个数组来存储可绘制文件夹中的图像? [英] How to write an array which stores the images from the drawable folder?
问题描述
我能知道我做错了什么吗?我只是想从可绘制文件夹中获取图像,然后将其传递到我的适配器以显示在我的gridView上.但是我不知道如何在数组内添加图像.错误消息在此行
Can i know what did i do wrong? i just wanna get my images from drawable folder and pass it to my adapter to display on my gridView.. But i dont know how to add inside the array.. The error message is at the line
arrImages[i] = this.getResources().getIdentifier(wd.getName(), "drawable", this.getPackageName());
任何人都想要帮助我:(我想根据数据库中的名称动态加载它.数据库名称(wd.getName())与我的文件名.png相同
Anybody wants to help me :( I wanna load it dynamically according to the names in my database.. the database name (wd.getName()) is the same as my file name.png
在顶部声明:
int arrImages [] = {};
在onCreate内部:
Inside onCreate :
int i = 0;
for (Words wd : words)
{
String log = "Id: " + wd.getId() + " ,Name: " + wd.getName();
// Writing Contacts to log
Log.d("wordList : ", log);
arrImages[i] = this.getResources().getIdentifier(wd.getName(), "drawable", this.getPackageName());
i++;
}
推荐答案
我已经使用动态方式完成了此操作.
I've done it using dynamic way.
实现新的数组列表:
ArrayList<Integer> arrImages = new ArrayList<>();
然后我可以将所有图像动态存储在内部.
Then I can store all my images dynamically inside.
这是我数据库中的循环,将可绘制文件夹中的资源ID添加到我的数组中:
This is the looping from my database, adding the resource id from drawable folder to my array :
for (Words wd : words)
{
String log = "Id: " + wd.getId() + " ,Name: " + wd.getName() ;
// Writing Contacts to log
Log.d("wordList : ", log);
arrImages.add( this.getResources().getIdentifier(wd.getName(), "drawable", this.getPackageName()));
}
这篇关于如何编写一个数组来存储可绘制文件夹中的图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!