自定义适配器浏览SD卡目录下的所有文件

如果在下载时需要更改下载文件的保存目录,或者浏览SD内的文件,如果不想单纯地用一个比较简单的listview来显示,做到有图标,有返回上一层目录,有返回根目录,那么自定义适配器可以很轻松的实现这一功能。

01       classMyAdapterextendsBaseAdapter {
02        privateLayoutInflater mInflater;
03        privateBitmap mIcon1;
04        privateBitmap mIcon2;
05        privateBitmap mIcon3;
06        privateBitmap mIcon4;
07        privateList<String> items;
08        privateList<String> paths;
09
10        publicMyAdapter(Context context, List<String> it, List<String> pa) {
11            mInflater = LayoutInflater.from(context);
12            items = it;
13            paths = pa;
14            mIcon1 = BitmapFactory.decodeResource(context.getResources(),
15                    R.drawable.back01);
16            mIcon2 = BitmapFactory.decodeResource(context.getResources(),
17                    R.drawable.back02);
18            mIcon3 = BitmapFactory.decodeResource(context.getResources(),
19                    R.drawable.folder);
20            mIcon4 = BitmapFactory.decodeResource(context.getResources(),
21                    R.drawable.doc);
22        }
23
24        @Override
25        publicintgetCount() {
26            // TODO Auto-generated method stub
27            returnitems.size();
28        }
29
30        @Override
31        publicObject getItem(intposition) {
32            // TODO Auto-generated method stub
33            returnitems.get(position);
34        }
35
36        @Override
37        publiclonggetItemId(intposition) {
38            // TODO Auto-generated method stub
39            returnposition;
40        }
41
42        @Override
43        publicView getView(intposition, View convertView, ViewGroup par) {
44            ViewHolder holder;
45            if(convertView ==null) {
46                convertView = mInflater.inflate(R.layout.file_row,null);
47                holder =newViewHolder();
48                holder.text = (TextView) convertView
49                        .findViewById(R.id.txt_path);
50                holder.icon = (ImageView) convertView
51                        .findViewById(R.id.icon_file_list1);
52                convertView.setTag(holder);
53            }else{
54                holder = (ViewHolder) convertView.getTag();
55            }
56            File f =newFile(paths.get(position).toString());
57            if(("b1").equals(items.get(position).toString())) {
58                holder.text.setText("Back to /");
59                holder.icon.setImageBitmap(mIcon1);
60            }elseif(("b2").equals(items.get(position).toString())) {
61                holder.text.setText("Back to ..");
62                holder.icon.setImageBitmap(mIcon2);
63            }else{
64                holder.text.setText(f.getName());
65                if(f.isDirectory()) {
66                    holder.icon.setImageBitmap(mIcon3);
67                }else{
68                    holder.icon.setImageBitmap(mIcon4);
69                }
70            }
71            returnconvertView;
72        }
73
74        publicfinalclassViewHolder {
75            publicTextView text;
76            publicImageView icon;
77        }
78    }

 

你可能感兴趣的:(android)