Reputation: 1769
I want to add all item from photo's folder to arraylist and below is my code -
here is my model
public class Model {
private String image;
public String getImage() {
return image;
}
public void setImage(String image) {
this.image = image;
}
}
here is my activity
public class MainActivity extends Activity {
private Model model;
private ArrayList<Model> alPhoto;
private File file;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
alPhoto = new ArrayList<Model>();
String root_sd = Environment.getExternalStorageDirectory().toString();
file = new File(root_sd + "/photo/");
File list[] = file.listFiles();
for (int i = 0; i < list.length; i++) {
// alPhoto.add(list[i].getName());
model = new Model();
model.setImage(alPhoto.get(i).getImage());
alPhoto.add(model);
Log.e("Load image from sd card******* : ", "Loading...." + alPhoto.get(i));
}
}
}
Upvotes: 0
Views: 4236
Reputation: 1769
Finally I got the solution.
for (int i = 0; i < list.length; i++)
{
String strPath = list[i].getAbsolutePath();
Log.e("Checking path",">>"+strPath);
Model model = new Model();
model.setImage(strPath);
alPhoto.add(model);
Log.e("Checking arraylist",">>"+alPhoto);
}
Upvotes: 1
Reputation: 1039
The problem is in your for a loop. You declare model globally so which is added only the last item in list is your problem instead of declare locally in for loop
for (int i = 0; i < list.length; i++) {
// alPhoto.add(list[i].getName());
Model model = new Model();
model.setImage(alPhoto.get(i).getImage());
alPhoto.add(model);
Log.e("Load image from sd card******* : ", "Loading...." + alPhoto.get(i));
}
Upvotes: 0