Android通过LIstView显示文件列表的两种方法介绍
时间:2022-05-26 00:00:39
在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卡中的文件列表时就失败了,后来就不爱去碰它,不爱去解决这个问题。导致这个问题一直没有解决,后来是迫于考试没法就去重新做,才发现其实没有什么问题,一直做下来都很顺畅。这使我明白了可怕的不是问题,而是没有去解决问题的恒心和懒惰的心理。其实有的问题它其实只是很简单的问题只要轻轻松松的就解决了,而不是什么重大的问题。在日常生活和学习中我们应该简单的看待问题。
标签:Android,LIstView,文件列表
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Android使用自定义View实现360手机卫士波浪球进度的效果
2023-03-21 19:18:34
![](https://img.aspxhome.com/file/2023/0/137250_0s.gif)
Java操作pdf的工具类itext的处理方法
2023-07-14 11:51:53
Android 优雅的读写Excel
2021-09-19 15:10:37
Android中AsyncTask与handler用法实例分析
2023-12-04 00:01:52
![](https://img.aspxhome.com/file/2023/8/88128_0s.png)
Java反射机制及Method.invoke详解
2023-11-22 17:25:28
MybatisPlus中@TableField注解的使用详解
2021-11-01 23:05:35
![](https://img.aspxhome.com/file/2023/0/82040_0s.png)
关于jdk环境变量的配置方式解读
2023-04-22 14:53:05
![](https://img.aspxhome.com/file/2023/6/61306_0s.png)
AccessibilityService实现微信发红包功能
2023-06-23 03:51:53
C++与namespace有关的两个编译错误的讲解
2021-12-09 11:37:35
Java 通过API操作GraphQL
2021-12-24 03:21:49
理解Java当中的回调机制(翻译)
2023-03-15 04:21:00
![](https://img.aspxhome.com/file/2023/0/89610_0s.png)
SpringBoot项目依赖和配置最新示例讲解
2021-08-13 23:04:23
java对象转型实例分析
2021-12-19 14:14:15
![](https://img.aspxhome.com/file/2023/4/101914_0s.png)
Java 守护线程_动力节点Java学院整理
2023-11-28 07:51:14
![](https://img.aspxhome.com/file/2023/7/60177_0s.png)
Android通过Movie展示Gif格式图片
2023-06-16 19:29:38
C#中改变DataGridView控件边框颜色的方法
2022-07-20 06:00:46
android简易文件管理器实例(列表式文件目录)
2021-07-28 09:43:30
![](https://img.aspxhome.com/file/2023/5/130455_0s.jpg)
SpringMVC深入讲解文件的上传下载实现
2022-04-21 09:01:51
![](https://img.aspxhome.com/file/2023/8/71648_0s.png)
c#中DataTable转List的2种方法示例
2022-05-10 20:51:09
解决IntelliJ IDEA中鼠标拖动选择为矩形区域问题
2022-04-03 21:08:51
![](https://img.aspxhome.com/file/2023/6/65176_0s.png)