SD卡文件浏览器_第1页
SD卡文件浏览器_第2页
SD卡文件浏览器_第3页
SD卡文件浏览器_第4页
SD卡文件浏览器_第5页
已阅读5页,还剩10页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

SD卡文件浏览器2<!--运行后,将会获取SD卡下全部文件、文件夹,并使用ListView将他们显示出来,当单击ListView的指定列表项时,将会显示该列表项下全部文件和文件夹-->

任务陈述3<!--新建一个Android工程,命名为SDFileExplorer--><!--在layout下添加添加两个使用线性布局技术的布局文件,分别为main.xml和line.xml--><!--main.xml布局文件源代码如下:--><LinearLayout

xmlns:android="/apk/res/android"

android:orientation="vertical"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

>任务实施4<!--显示当前路径的的文本框--><TextView

android:id="@+id/path"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:layout_gravity="center_horizontal"

/><!--显示当前路径的的文本框--><ListView

android:id="@+id/list"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:divider="#000"

/>任务实施5<!--返回上一级目录的按钮--><Button

android:id="@+id/parent"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:background="@drawable/home"

android:paddingTop="20dp"

android:layout_gravity="center"/><!--line.xml布局文件源代码如下:--><LinearLayout

xmlns:android="/apk/res/android"

android:orientation="horizontal"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

>任务实施6<!--定义一个ImageView,用于作为列表项的一部分--><ImageView

android:id="@+id/icon"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:paddingLeft="10dp"/><!--定义一个TextView,用于作为列表项的一部分--><TextView

android:id="@+id/file_name"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:textSize="16dp"

android:gravity="center_vertical"

android:paddingLeft="10dp"

android:paddingTop="10dp"

android:paddingBottom="10dp"

/>任务实施7<!--在MainActivity.java下声明如下变量,并导入相关包-->ListView

listView;TextView

textView;File

currentParent;File[]

currentFiles;<!--在onCreate方法中声明并获取布局文件中ListView和TextView控件-->listView

=(ListView)findViewById(R.id.list);textView

=(TextView)findViewById(R.id.path);任务实施8<!--在onCreate方法中添加如下代码,用于显示SD卡中的文件和文件夹列表-->Fileroot=

new

File("/mnt/sdcard/");if

(root.exists())

{

currentParent

=root;

currentFiles

=root.listFiles();

inflateListView(currentFiles);}任务实施9<!--在MainActivity.java中添加显示文件及文件夹名称到ListView中的方法inflateListView--><!--创建一个List集合,List集合的元素是Map->List<Map<String,Object>>listItems=

new

ArrayList<Map<String,Object>>();

for

(int

i=0;i<files.length;i++)

{

Map<String,Object>listItem=

new

HashMap<String,Object>();任务实施10<!--如果当前File是文件夹,使用folder图标;否则使用file图标-->if

(files[i].isDirectory())

{

listItem.put("icon",R.drawable.folder);

}

else

{

listItem.put("icon",R.drawable.file);

}

listItem.put("fileName",files[i].getName());任务实施11<!--添加List项-->

listItems.add(listItem);<!--创建一个SimpleAdapter-->SimpleAdaptersimpleAdapter=

new

SimpleAdapter(this,listItems,

R.layout.line,

new

String[]{

"icon",

"fileName"

},

new

int[]{

R.id.icon,R.id.file_name

});<!--为ListView设置Adapter->listView.setAdapter(simpleAdapter);

textView.setText("当前路径为:"

+

currentParent.getCanonicalPath());任务实施12<!--在OnCreate方法中为ListView的列表项的单击事件绑定监听器-->listView.setOnItemClickListener(new

OnItemClickListener()

{

public

void

onItemClick(AdapterView<?>parent,Viewview,int

position,

long

id)<!--用户单击了文件,直接返回,不做任何处理-->if

(currentFiles[position].isFile())

return;<!--获取用户点击的文件夹下的所有文件-->File[]tmp=

currentFiles[position].listFiles();

if

(tmp==

null

||tmp.length

==0)

{

Toast.makeText(SDFileExplorer.this,

"当前路径不可访问或该路径下没有文件",

20000).show();

}任务实施13<!--获取用户单击的列表项对应的文件夹,设为当前的父文件夹-->currentParent

=

currentFiles[position];<!--保存当前的父文件夹内的全部文件和文件夹-->currentFiles

=tmp;<!--再次更新ListView-->inflateListView(currentFiles);任务实施14<!--在OnCreate方法中获取上一级目录按钮,并为其添加监听器-->

Buttonparent=(Button)findViewById(R.id.parent);

parent.setOnClickListener(new

OnClickListener()

public

void

onClick(Viewsource)

{

if

(!currentParent.getCanonicalPath().equals("/mnt/sdcard"))

{

//

获取上一级目录

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

最新文档

评论

0/150

提交评论