Android通過LIstView顯示文件列表


綏江一百http://www.sj100.net                                                  歡迎,進入綏江一百感謝點擊[我的小網站,請大家多多指教相互共勉]

      在Android中通過ListView顯示SD卡中的文件列表一共有兩種方法,一是:通過繼承ListActivity顯示;二是:利用BaseAdapter顯示。BaseAdapter是一個公共基類適配器,用於對ListView和Spinner等 一些控件提供顯示數據。下面是利用BaseAdapter類來實現通過LIstView顯示SD卡的步驟:

1.main.xml界面設計,如下圖

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     android:layout_width="fill_parent"
 4     android:layout_height="fill_parent"
 5     android:orientation="vertical" >
 6 
 7     <TextView
 8         android:id="@+id/Txt_Path"
 9         android:layout_width="fill_parent"
10         android:layout_height="wrap_content"
11         android:text="@string/hello" />
12 
13     <Button
14         android:id="@+id/But_Up"
15         android:layout_width="wrap_content"
16         android:layout_height="wrap_content"
17         android:text="向上" />
18 
19     <ListView
20         android:id="@+id/List_View"
21         android:layout_width="match_parent"
22         android:layout_height="wrap_content" >
23 
24     </ListView>
25 
26 </LinearLayout>
main.xml

 2.item.xml界面設計,如下圖

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     android:id="@+id/RelativeLayout1"
 4     android:layout_width="match_parent"
 5     android:layout_height="match_parent"
 6     android:orientation="vertical" >
 7 
 8     <TextView
 9         android:id="@+id/Txt_Size"
10         android:layout_width="wrap_content"
11         android:layout_height="wrap_content"
12         android:layout_alignParentRight="true"
13         android:layout_alignParentTop="true"
14         android:text="TextView" />
15 
16     <ImageView
17         android:id="@+id/image_ico"
18         android:layout_width="wrap_content"
19         android:layout_height="wrap_content"
20         android:layout_alignParentLeft="true"
21         android:layout_below="@+id/Txt_Size"
22         android:layout_marginLeft="18dp"
23         android:src="@drawable/folder" />
24 
25     <TextView
26         android:id="@+id/Txt_Name"
27         android:layout_width="wrap_content"
28         android:layout_height="wrap_content"
29         android:layout_alignBottom="@+id/image_ico"
30         android:layout_alignParentRight="true"
31         android:text="TextView" />
32 
33 </RelativeLayout>
item.xml

效果圖main.xml

效果圖item.xml

 3.File_Adter類的實現

 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     }
69     public static String getFilesSize(File f)
70     {
71         int sub_index=0;
72         String show="";
73         if(f.isFile())
74         {
75             long length=f.length();
76         if(length>=1073741824)
77         {
78             sub_index=String.valueOf((float)length/1073741824).indexOf(".");
79             show=((float)length/1073741824+"000").substring(0,sub_index+3)+"GB";
80         }
81         else if(length>=1048576)
82         {
83             sub_index=(String.valueOf((float)length/1048576)).indexOf(".");
84             show=((float)length/1048576+"000").substring(0,sub_index+3)+"GB";
85         }
86         else if(length>=1024)
87         {
88             sub_index=(String.valueOf((float)length/1024)).indexOf(".");
89             show=((float)length/1024+"000").substring(0,sub_index+3)+"GB";
90         }
91         else if(length<1024)
92             show=String.valueOf(length)+"B";
93         }
94             return show;
95     }
96 }
File_Adter.java

 4.File_listActivity的實現

 1 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 }
File_listActivity.java

 效果圖展示 

總結

     在做這個File_Adter的時候,需要注意的有三點,一是在新建文件列表類的時候要繼承BaseAdapter,並且一定不要勾選主方法。二是要在res\drawable-hdpi中添加用於顯示文件和文件夾的圖片。三是在item.xml的設計時需把Change Layout中New Layout Type的值設為LinearLayout,這樣就方便我們隨意放置ImageView和textView的位置,從而有利於軟件的美觀。第一次做這個顯示SD卡中的文件列表時就失敗了,后來就不愛去碰它,不愛去解決這個問題。導致這個問題一直沒有解決,后來是迫於考試沒法就去重新做,才發現其實沒有什么問題,一直做下來都很順暢。這使我明白了可怕的不是問題,而是沒有去解決問題的恆心和懶惰的心理。其實有的問題它其實只是很簡單的問題只要輕輕松松的就解決了,而不是什么重大的問題。在日常生活和學習中我們應該簡單的看待問題。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM