萬盛學電腦網

 萬盛學電腦網 >> 網絡編程 >> 安卓開發 >> 自定義適配器浏覽SD卡目錄下的所有文件

自定義適配器浏覽SD卡目錄下的所有文件

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

copyright © 萬盛學電腦網 all rights reserved