In my activity I implement a list which contains the names of some files. Every list\'s item refers to a layout in which I\'d like to show the name of the images and a thumb
First: You will have to change your ArrayAdapter to BaseAdapter because BaseAdapter gives you more flexibility with the views. Try to use this code and see how it works:
Here's how I am getting images from sd card and populating it in a listview
public class GetSdCardContent extends Activity {
public static Cursor cursor;
private int columnIndex;
private File file;
private String SD_CARD_ROOT;
ArrayList f = new ArrayList();
File[] listFile;
ImageAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.sdcard_layout);
getSdcardImages();
ListView lv1 = (ListView) findViewById(R.id.sdlistView1);
adapter = new ImageAdapter();
lv1.setAdapter(adapter);
}
public void getSdcardImages() {
File file = new File(
android.os.Environment.getExternalStorageDirectory().getPath(),
"Pictures");
if (file.isDirectory()) {
listFile = file.listFiles();
for (int i = 0; i < listFile.length; i++) {
f.add(listFile[i].getAbsolutePath());
Log.i("FILES:"+"---", f.toString());
}
}
}
This is the adapter I am using. It's a base adapter and I am getting only images for now. You can also add your TextView if you want to.
public class ImageAdapter extends BaseAdapter {
private LayoutInflater mInflater;
public ImageAdapter() {
mInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public int getCount() {
return f.size();
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
holder = new ViewHolder();
convertView = mInflater.inflate(R.layout.custom_listview_items,
null);
holder.imageview = (ImageView) convertView
.findViewById(R.id.customimageView1);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
Bitmap myBitmap = BitmapFactory.decodeFile(f.get(position));
try {
holder.imageview.setImageBitmap(getResizedBitmap(myBitmap, 300, 300));
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return convertView;
}
}
This method is for bitmap. If you want to resize your image and stuff.
public Bitmap getResizedBitmap(Bitmap bm, int newHeight, int newWidth) {
int width = bm.getWidth();
int height = bm.getHeight();
float scaleWidth = ((float) newWidth) / width;
float scaleHeight = ((float) newHeight) / height;
// CREATE A MATRIX FOR THE MANIPULATION
Matrix matrix = new Matrix();
// RESIZE THE BIT MAP
matrix.postScale(scaleWidth, scaleHeight);
// "RECREATE" THE NEW BITMAP
Bitmap resizedBitmap = Bitmap.createBitmap(bm, 0, 0, width, height, matrix, false);
return resizedBitmap;
}
class ViewHolder {
ImageView imageview;
}
}
Last part: Two layouts.
One: layout for items.
Second: Main Layout. (ListView)
Hope this gives some help..:)