Reputation: 1127
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
.
Upvotes: 0
Views: 461
Reputation: 1294
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]"
Upvotes: 0
Reputation: 234795
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);
}
}
Upvotes: 2
Reputation: 16526
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));
}
Upvotes: 1