网络112  陈永森

   下面是通过BaseAdapter类显示文件列表:

   界面设计如下:

     

添加BaseAdapter类,里面的代码如下:

 1 package com.cqvie;
 2
3 import java.io.File;
4 import java.util.LinkedList;
5 import java.util.List;
6
7 import android.app.Activity;
8 import android.graphics.Bitmap;
9 import android.graphics.BitmapFactory;
10 import android.view.View;
11 import android.view.ViewGroup;
12 import android.widget.BaseAdapter;
13 import android.widget.ImageView;
14 import android.widget.TextView;
15
16 public class File_Adter extends BaseAdapter {
17     public Activity activity;  //创建View时必须要提供Context
18     public List<File> list=new LinkedList<File>();  //数据源(文件列表)
19     public String currPath;//当前路径
20     private Bitmap bmp_folder,bmp_file;
21    
22     public int getCount() {
23         // TODO Auto-generated method stub
24         return list.size();
25     }
26
27     public Object getItem(int arg0) {
28         // TODO Auto-generated method stub
29         return null;
30     }
31
32     public long getItemId(int position) {
33         // TODO Auto-generated method stub
34         return position;
35     }
36
37     public View getView(int position, View arg1, ViewGroup arg2) {
38         // TODO Auto-generated method stub
39         View v=View.inflate(activity,R.layout.item,null);
40         TextView Txt_Name=(TextView) v.findViewById(R.id.Txt_Name);
41         TextView Txt_Size=(TextView) v.findViewById(R.id.Txt_Size);
42         ImageView img=(ImageView) v.findViewById(R.id.image_ico);
43         File f=list.get(position);
44         Txt_Name.setText(f.getName());
45         Txt_Size.setText(getFilesSize(f));
46         if(f.isDirectory())
47              img.setImageBitmap(bmp_folder);
48         else
49              img.setImageBitmap(bmp_file);
50              return v;
51     }
52     public void scanFiles(String path)
53     {
54         list.clear();
55         File dir=new File(path);
56         File[] subFiles=dir.listFiles();
57         if(subFiles!=null)
58            for(File f:subFiles)
59                    list.add(f);
60         this.notifyDataSetChanged();
61         currPath=path;
62     }
63     public File_Adter(Activity activity)
64     {
65         this.activity=activity;
66         bmp_folder=BitmapFactory.decodeResource(activity.getResources(),R.drawable.folder);//文件夹,decodeResource图片解码,source资源,解码为Bitmap类型;
67         bmp_file=BitmapFactory.decodeResource(activity.getResources(),R.drawable.file);//文件
68     }

 

FileListViewActivity里的代码如下:

package com.cqvie;
2
3 import java.io.File;
4
5 import android.app.Activity;
6 import android.os.Bundle;
7 import android.view.View;
8 import android.view.View.OnClickListener;
9 import android.widget.AdapterView;
10 import android.widget.AdapterView.OnItemClickListener;
11 import android.widget.Button;
12 import android.widget.ListView;
13 import android.widget.TextView;
14
15 public class File_listActivity extends Activity implements OnItemClickListener, OnClickListener {
16     /** Called when the activity is first created. */
17     @Override
18     public void onCreate(Bundle savedInstanceState) {
19         super.onCreate(savedInstanceState);
20         setContentView(R.layout.main);
21        
22         List_View=(ListView) findViewById(R.id.List_View);
23         But_Up=(Button) findViewById(R.id.But_Up);
24         Txt_Path=(TextView) findViewById(R.id.Txt_Path);
25         File_Adter Adter=new File_Adter(this);
26         List_View.setAdapter(Adter);
27         List_View.setOnItemClickListener(this);
28         Adter.scanFiles("/");
29         But_Up.setOnClickListener(this);
30     }
31     ListView List_View;
32     TextView Txt_Path;
33     Button But_Up;
34     public void onClick(View v) {
35         // TODO Auto-generated method stub
36         File_Adter ad=(File_Adter) List_View.getAdapter();
37         if(ad.currPath.equals("/")) return;
38         File f=new File(ad.currPath);
39         Txt_Path.setText(f.getParent());
40         ad.scanFiles(f.getParent());
41        
42     }
43     public void onItemClick(AdapterView<?> parent, View v, int positiong, long id) {
44         // TODO Auto-generated method stub
45         File_Adter da=(File_Adter) List_View.getAdapter();
46         File f=da.list.get(positiong);
47         if(f.isDirectory())
48         {
49             Txt_Path.setText(f.getPath());
50             da.scanFiles(f.getPath());
51         }
52     }
53 }

 

 

实现结果如下: