Android通过LIstView显示文件列表的两种方法介绍

所属分类: 软件编程 / Android 阅读数: 1175
收藏 0 赞 0 分享
在Android中通过ListView显示SD卡中的文件列表一共有两种方法,一是:通过继承ListActivity显示;二是:利用BaseAdapter显示。BaseAdapter是一个公共基类适配器,用于对ListView和Spinner等 一些控件提供显示数据。下面是利用BaseAdapter类来实现通过LIstView显示SD卡的步骤:

1.main.xml界面设计,如下图
复制代码 代码如下:

<?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="向上" />
<ListView
android:id="@+id/List_View"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
main.xml

2.item.xml界面设计,如下图
复制代码 代码如下:

<?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.File_Adter类的实现
复制代码 代码如下:

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 {
public Activity activity; //创建View时必须要提供Context
public List<File> list=new LinkedList<File>(); //数据源(文件列表)
public String currPath;//当前路径
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);//文件夹,decodeResource图片解码,source资源,解码为Bitmap类型;
bmp_file=BitmapFactory.decodeResource(activity.getResources(),R.drawable.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.File_listActivity的实现
复制代码 代码如下:

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


效果图展示 
总结
在做这个File_Adter的时候,需要注意的有三点,一是在新建文件列表类的时候要继承BaseAdapter,并且一定不要勾选主方法。二是要在res\drawable-hdpi中添加用于显示文件和文件夹的图片。三是在item.xml的设计时需把Change Layout中New Layout Type的值设为LinearLayout,这样就方便我们随意放置ImageView和textView的位置,从而有利于软件的美观。第一次做这个显示SD卡中的文件列表时就失败了,后来就不爱去碰它,不爱去解决这个问题。导致这个问题一直没有解决,后来是迫于考试没法就去重新做,才发现其实没有什么问题,一直做下来都很顺畅。这使我明白了可怕的不是问题,而是没有去解决问题的恒心和懒惰的心理。其实有的问题它其实只是很简单的问题只要轻轻松松的就解决了,而不是什么重大的问题。在日常生活和学习中我们应该简单的看待问题。
更多精彩内容其他人还在看

使用ViewPager实现android软件使用向导功能实现步骤

现在的大部分android软件,都是使用说明,就是第一次使用该软件时,会出现向导,可以左右滑动,然后就进入应用的主界面了,下面我们就实现这个功能
收藏 0 赞 0 分享

android在异步任务中关闭Cursor的代码方法

android在异步任务中如何关闭Cursor?在我们开发应用的时候,很多时候会遇到这种问题,下面我们就看看代码如何实现
收藏 0 赞 0 分享

Android自定义桌面功能代码实现

android自定义桌面其实很简单,看一个例子就明白了
收藏 0 赞 0 分享

android将图片转换存到数据库再从数据库读取转换成图片实现代码

有时候我们想把图片存入到数据库中,尽管这不是一种明智的选择,但有时候还是不得以会用到,下面说说将图片转换成byte[]数组存入到数据库中去,并从数据库中取出来转换成图像显示出来
收藏 0 赞 0 分享

TextView显示系统时间(时钟功能带秒针变化

用System.currentTimeMillis()可以获取系统当前的时间,我们可以开启一个线程,然后通过handler发消息,来实时的更新TextView上显示的系统时间,可以做一个时钟的功能
收藏 0 赞 0 分享

Android用ListView显示SDCard文件列表的小例子

本文简单实现了用ListView显示SDCard文件列表,目录的回退等功能暂不讨论,获取文件列表,files即为所选择目录下的所有文件列表
收藏 0 赞 0 分享

Android拦截外拨电话程序示例

这篇文章主要介绍了Android拦截外拨电话的示例,大家参考使用吧
收藏 0 赞 0 分享

通过Html网页调用本地安卓(android)app程序代码

如何使用html网页和本地app进行传递数据呢?经过研究,发现还是有方法的,总结了一下,大致有一下几种方式
收藏 0 赞 0 分享

android Textview文字监控(Textview使用方法)

以手机号充值为例,当用户输入最后一位数时候,进行汇率的变换,本文就实现类似这样的功能
收藏 0 赞 0 分享

Android ListView长按弹出菜单二种实现方式示例

这篇文章主要介绍了Android ListView长按弹出菜单的方法,大家参考实现
收藏 0 赞 0 分享
查看更多