Android introduces two ways to display a list of files by ListView _android

Source: Internet
Author: User
Tags stub
The list of files in the SD card is displayed through ListView in Android there are two methods, one is: Through the inheritance listactivity display, the second is: use Baseadapter display. Baseadapter is a common base class adapter that provides display data for some controls, such as ListView and spinner. Here are the steps to use the Baseadapter class to display the SD card via ListView:

1.main.xml interface design, as shown below
Copy Code code 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 below
Copy Code code 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


Effect Diagram Main.xml

Effect Diagram Item.xml
Implementation of 3.file_adter Class
Copy Code code as follows:

Package Com.cqvie;
Import Java.io.File;
Import java.util.LinkedList;
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 {
The activity of the public activity; You must provide a context when you create a view
Public list<file> list=new linkedlist<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 for 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+). substring (0,sub_index+3) + "GB";
}
else if (length>=1048576)
{
sub_index= (string.valueof (float) length/1048576). IndexOf (".");
Show= ((float) length/1048576+). substring (0,sub_index+3) + "GB";
}
else if (length>=1024)
{
sub_index= (string.valueof (float) length/1024). IndexOf (".");
Show= ((float) length/1024+). substring (0,sub_index+3) + "GB";
}
else if (length<1024)
Show=string.valueof (length) + "B";
}
return to show;
}
}
File_adter.java

The realization of 4.file_listactivity
Copy Code code 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 the activity is a. */
@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


Effect Chart Show
Summary
In doing this file_adter, you need to note that there are three points, one is in the new file list class to inherit Baseadapter, and must not check the main method. The second is to add a picture to display files and folders in res\drawable-hdpi. Thirdly, in the design of item.xml, we need to set the value of the new Layout type in the change Layout to LinearLayout, so that we can conveniently place the ImageView and TextView position, which is beneficial to the beauty of the software. The first time you do this shows the list of files in the SD card failed, then do not love to touch it, do not love to solve this problem. Lead to this problem has not been resolved, and then was forced to go to the exam can not be done again, only to find that there is no problem, has been done very smoothly. This makes me understand that the terrible is not the problem, but not to solve the problem of perseverance and lazy psychology. In fact, some of the problems it is just a very simple question as long as the easy to solve, rather than a major problem. In daily life and study we should look at the problem simply.
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.