自定义适配器浏览SD卡目录下的所有文件
2012-11-21 14:02:53 来源:WEB开发网 class MyAdapter extends BaseAdapter {
private LayoutInflater mInflater;
private Bitmap mIcon1;
private Bitmap mIcon2;
private Bitmap mIcon3;
private Bitmap mIcon4;
private List<String> items;
private List<String> paths;
public MyAdapter(Context context, List<String> it, List<String> pa) {
mInflater = LayoutInflater.from(context);
items = it;
paths = pa;
mIcon1 = BitmapFactory.decodeResource(context.getResources(),
R.drawable.back01);
mIcon2 = BitmapFactory.decodeResource(context.getResources(),
R.drawable.back02);
mIcon3 = BitmapFactory.decodeResource(context.getResources(),
R.drawable.folder);
mIcon4 = BitmapFactory.decodeResource(context.getResources(),
R.drawable.doc);
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return items.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return items.get(position);
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup par) {
ViewHolder holder;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.file_row, null);
holder = new ViewHolder();
holder.text = (TextView) convertView
.findViewById(R.id.txt_path);
holder.icon = (ImageView) convertView
.findViewById(R.id.icon_file_list1);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
File f = new File(paths.get(position).toString());
if (("b1").equals(items.get(position).toString())) {
holder.text.setText("Back to /");
holder.icon.setImageBitmap(mIcon1);
} else if (("b2").equals(items.get(position).toString())) {
holder.text.setText("Back to ..");
holder.icon.setImageBitmap(mIcon2);
} else {
holder.text.setText(f.getName());
if (f.isDirectory()) {
holder.icon.setImageBitmap(mIcon3);
} else {
holder.icon.setImageBitmap(mIcon4);
}
}
return convertView;
}
public final class ViewHolder {
public TextView text;
public ImageView icon;
}
}
更多精彩
赞助商链接