




版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
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. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 浙江旅游职业学院《现代汉语语法研究》2023-2024学年第二学期期末试卷
- 武汉纺织大学《生物化学实验》2023-2024学年第二学期期末试卷
- 莱芜职业技术学院《卫生微生物学A》2023-2024学年第二学期期末试卷
- 江西科技师范大学《汉语语音及教学》2023-2024学年第二学期期末试卷
- 石家庄财经职业学院《中国古代文学A(IV)》2023-2024学年第二学期期末试卷
- 泉州职业技术大学《数学课程标准与教材研究》2023-2024学年第二学期期末试卷
- 贵州工程应用技术学院《音乐作品欣赏》2023-2024学年第二学期期末试卷
- 辽阳职业技术学院《古希腊美学》2023-2024学年第二学期期末试卷
- 河北科技学院《影视声音》2023-2024学年第二学期期末试卷
- 书画考试真题及答案解析
- 2025年高考语文全国一卷试题真题及答案详解(精校打印)
- 2025年北京海淀初三二模语文试题及答案
- 2024年成都市八年级(初二会考)中考地理+生物真题试卷
- 2024北京海淀区四年级(下)期末数学试题及答案
- 2025年江苏盐城市燕舞集团有限公司招聘笔试参考题库含答案解析
- 【MOOC】运输包装-暨南大学 中国大学慕课MOOC答案
- 2023年高考真题-生物(辽宁卷) 含答案
- (新教材)高中数学A版选择性必修第三册知识点
- DL∕T 5783-2019 水电水利地下工程地质超前预报技术规程
- 星期音乐会智慧树知到期末考试答案章节答案2024年同济大学
- 生命哲学:爱、美与死亡智慧树知到期末考试答案2024年
评论
0/150
提交评论