手机
当前位置:查字典教程网 >编程开发 >安卓软件开发 >Android通过LIstView显示文件列表的两种方法介绍
Android通过LIstView显示文件列表的两种方法介绍
摘要:在Android中通过ListView显示SD卡中的文件列表一共有两种方法,一是:通过继承ListActivity显示;二是:利用BaseA...

在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

1

效果图main.xml

2

效果图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

3

效果图展示

总结

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

【Android通过LIstView显示文件列表的两种方法介绍】相关文章:

android中查看项目数字证书的两种方法

Android 设置应用全屏的两种解决方法

android ListView的右边滚动滑块启用方法 分享

Android开发笔记 改变字体颜色的三种方法

android真机调试时无法显示logcat信息的解决方法介绍

Android 工程内嵌资源文件的两种方法

Android的Service应用程序组件基本编写方法

Android开发之XML文件解析的使用

Android获取本机电话号码的简单方法

Android编程之客户端通过socket与服务器通信的方法

精品推荐
分类导航