WEB开发网
开发学院手机开发Android 开发 自定义适配器浏览SD卡目录下的所有文件 阅读

自定义适配器浏览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;p

 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;
}
}

Tags:定义 适配器 浏览

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接