如果在下載時需要更改下載檔案的保存目錄,或者流覽SD內的檔,如果不想單純地用一個比較簡單的listview來顯示,做到有圖示,有返回上一層目錄,有返回根目錄,那麼自訂配接器可以很輕鬆的實現這一功能。

 

01 classMyAdapterextendsBaseAdapter {
02 privateLayoutInflater mInflater;
03 privateBitmap mIcon1;
04 privateBitmap mIcon2;
05 privateBitmap mIcon3;
06 privateBitmap mIcon4;
07 privateList items;
08 privateList paths;
09
10 publicMyAdapter(CoNtext coNtext, List it, List 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 }
創作者介紹
創作者 shadow 的頭像
shadow

資訊園

shadow 發表在 痞客邦 留言(0) 人氣()