In Android, there are two methods to display the file list in the SD card through ListView: Display by inheriting ListActivity; and display by using BaseAdapter. BaseAdapter is a common base class adapter used to provide display data for ListView, Spinner, and other controls. The following describes how to use the BaseAdapter class to display the SD card through LIstView:
1. main. xml interface design, as shown in figureCopy codeThe Code is as follows: <? Xml version = "1.0" encoding = "UTF-8"?>
<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
Android: orientation = "vertical">
<TextView
Android: id = "@ + id/Txt_Path"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: text = "@ string/hello"/>
<Button
Android: id = "@ + id/But_Up"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: text = "up"/>
<ListView
Android: id = "@ + id/List_View"
Android: layout_width = "match_parent"
Android: layout_height = "wrap_content">
</ListView>
</LinearLayout>
Main. xml
2. item. xml interface design, as shown in figureCopy codeThe Code is as follows: <? Xml version = "1.0" encoding = "UTF-8"?>
<RelativeLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Android: id = "@ + id/RelativeLayout1"
Android: layout_width = "match_parent"
Android: layout_height = "match_parent"
Android: orientation = "vertical">
<TextView
Android: id = "@ + id/Txt_Size"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: layout_alignParentRight = "true"
Android: layout_alignParentTop = "true"
Android: text = "TextView"/>
<ImageView
Android: id = "@ + id/image_ico"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: layout_alignParentLeft = "true"
Android: layout_below = "@ + id/Txt_Size"
Android: layout_marginLeft = "18dp"
Android: src = "@ drawable/folder"/>
<TextView
Android: id = "@ + id/Txt_Name"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: layout_alignBottom = "@ + id/image_ico"
Android: layout_alignParentRight = "true"
Android: text = "TextView"/>
</RelativeLayout>
Item. xml
Main. xml
Item. xml
3. Implementation of the File_Adter classCopy codeThe Code is as follows: package com. cqvie;
Import java. io. File;
Import java. util. Collections list;
Import java. util. List;
Import android. app. Activity;
Import android. graphics. Bitmap;
Import android. graphics. BitmapFactory;
Import android. view. View;
Import android. view. ViewGroup;
Import android. widget. BaseAdapter;
Import android. widget. ImageView;
Import android. widget. TextView;
Public class File_Adter extends BaseAdapter {
Public Activity activity; // you must provide Context when creating a View.
Public List <File> list = new external List <File> (); // data source (File list)
Public String currPath; // current path
Private Bitmap BMP _folder, BMP _file;
Public int getCount (){
// TODO Auto-generated method stub
Return list. size ();
}
Public Object getItem (int arg0 ){
// TODO Auto-generated method stub
Return null;
}
Public long getItemId (int position ){
// TODO Auto-generated method stub
Return position;
}
Public View getView (int position, View arg1, ViewGroup arg2 ){
// TODO Auto-generated method stub
View v = View. inflate (activity, R. layout. item, null );
TextView Txt_Name = (TextView) v. findViewById (R. id. Txt_Name );
TextView Txt_Size = (TextView) v. findViewById (R. id. Txt_Size );
ImageView img = (ImageView) v. findViewById (R. id. image_ico );
File f = list. get (position );
Txt_Name.setText (f. getName ());
Txt_Size.setText (getFilesSize (f ));
If (f. isDirectory ())
Img. setImageBitmap (BMP _folder );
Else
Img. setImageBitmap (BMP _file );
Return v;
}
Public void scanFiles (String path)
{
List. clear ();
File dir = new File (path );
File [] subFiles = dir. listFiles ();
If (subFiles! = Null)
For (File f: subFiles)
List. add (f );
This. notifyDataSetChanged ();
CurrPath = path;
}
Public File_Adter (Activity activity)
{
This. activity = activity;
BMP _folder = BitmapFactory. decodeResource (activity. getResources (), R. drawable. folder); // folder, decodeResource image decoding, source resource decoding, Bitmap type;
BMP _file = BitmapFactory. decodeResource (activity. getResources (), R. drawable. file); // file
}
Public static String getFilesSize (File f)
{
Int sub_index = 0;
String show = "";
If (f. isFile ())
{
Long length = f. length ();
If (length >=1073741824)
{
Sub_index = String. valueOf (float) length/1073741824). indexOf (".");
Show = (float) length/1073741824 + "000"). substring (0, sub_index + 3) + "GB ";
}
Else if (length >=1048576)
{
Sub_index = (String. valueOf (float) length/1048576). indexOf (".");
Show = (float) length/1048576 + "000"). substring (0, sub_index + 3) + "GB ";
}
Else if (length >=1024)
{
Sub_index = (String. valueOf (float) length/1024). indexOf (".");
Show = (float) length/1024 + "000"). substring (0, sub_index + 3) + "GB ";
}
Else if (length <1024)
Show = String. valueOf (length) + "B ";
}
Return show;
}
}
File_Adter.java
4. Implementation of File_listActivityCopy codeThe Code is as follows: package com. cqvie;
Import java. io. File;
Import android. app. Activity;
Import android. OS. Bundle;
Import android. view. View;
Import android. view. View. OnClickListener;
Import android. widget. AdapterView;
Import android. widget. AdapterView. OnItemClickListener;
Import android. widget. Button;
Import android. widget. ListView;
Import android. widget. TextView;
Public class File_listActivity extends Activity implements OnItemClickListener, OnClickListener {
/** Called when the activity is first created .*/
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );
List_View = (ListView) findViewById (R. id. List_View );
But_Up = (Button) findViewById (R. id. But_Up );
Txt_Path = (TextView) findViewById (R. id. Txt_Path );
File_Adter Adter = new File_Adter (this );
List_View.setAdapter (Adter );
List_View.setOnItemClickListener (this );
Adter. scanFiles ("/");
But_Up.setOnClickListener (this );
}
ListView List_View;
TextView Txt_Path;
Button But_Up;
Public void onClick (View v ){
// TODO Auto-generated method stub
File_Adter ad = (File_Adter) List_View.getAdapter ();
If (ad. currPath. equals ("/") return;
File f = new File (ad. currPath );
Txt_Path.setText (f. getParent ());
Ad. scanFiles (f. getParent ());
}
Public void onItemClick (AdapterView <?> Parent, View v, int positiong, long id ){
// TODO Auto-generated method stub
File_Adter da = (File_Adter) List_View.getAdapter ();
File f = da. list. get (positiong );
If (f. isDirectory ())
{
Txt_Path.setText (f. getPath ());
Da. scanFiles (f. getPath ());
}
}
}
File_listActivity.java
Display
Summary
When doing this File_Adter, you need to pay attention to three points. First, you must inherit the BaseAdapter when creating a file list class, And do not check the main method. The second is to add images used to display files and folders to res \ drawable-hdpi. The third is in item. when designing xml, we need to set the value of New Layout Type in Change Layout to LinearLayout, so that we can place the ImageView and textView positions at will, which is conducive to the appearance of the software. The first time I did this, I failed to display the list of files in the SD card. Later I didn't like to touch it, and I didn't like to solve this problem. As a result, this problem has not been solved. Later, I was forced to re-do the examination, so that I found that there was no problem and it was smooth. This makes me understand that what is terrible is not the problem, but the perseverance and laziness of solving the problem. In fact, some problems are just simple issues that can be easily solved, rather than major problems. In daily life and study, we should simply look at the problem.