Is it possible to generate a Drawable[]
with a for loop?
ArrayList:
List<String> list = new ArrayList<String>();
list.add(Environment.getExternalStorageDirectory() + "/App/posters/80348-16.jpg");
list.add(Environment.getExternalStorageDirectory() + "/App/posters/83462-8.jpg");
to
private Drawable[] mThumbIds = {
Drawable.createFromPath(Environment.getExternalStorageDirectory() + "/Treevo/posters/80348-16.jpg"), Drawable.createFromPath(Environment.getExternalStorageDirectory() + "/Treevo/posters/83462-8.jpg")
};
My idea:
for (int i = 0; i < list.size(); i++)
{
//But what I need to do here?
}
My goal is to generate the Items for a GridView
.
You can do this:
File dir = Environment.getExternalStorageDirectory();
List<File> list = new ArrayList<File>();
list.add(new File(dir, "App/posters/80348-16.jpg"));
list.add(new File(dir, "App/posters/83462-8.jpg"));
List<Drawable> mThumbs = new ArrayList<Drawable>();
for (File file : list) {
mThumbs.add(Drawable.createFromPath(file.getPath());
}
If you want to retrieve a list of all .jpg files in the folder, you can construct list
like this:
File posterDir = new File(Environment.getExternalStorageDirectory(),
"App/posters");
List<File> list = new ArrayList<File>();
for (File file : posterDir.listFiles()) {
if (file.isFile() && file.getName().endsWith(".png")) {
list.add(file);
}
}
No sure what kind of path it expects, but you could try Drawable.createFromPath method. Assuming you store in the first list the right paths, you're loop would look like this.-
Drawable[] drawables = new Drawable[list.size()]();
for (int i = 0; i < list.size(); i++) {
drawables[i] = Drawable.createFromPath(list.get(i));
}
What you need to do is a for each loop. It goes something like this.
List<String> list = new ArrayList<String>();
String[] imgPathList = ... // Add all of the "/App/poster..." strings here
for(String x : imgPathList) {
list.add(Enviroment.getExternalStorageDirectory() + x);
}
Here is an example with different things.
List<String> list = new ArrayList<String>();
String[] data = {
"123",
"456",
"789"
};
for(String x : data) {
list.add("Number: " + x);
}
System.out.println(list.toString());
Prints: "[Number: 123, Number: 456, Number: 789]"
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.