android用异步操作AsyncTask编写文件查看器
Activity程序
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.SimpleAdapter;
public class MainActivity extends Activity {
private ListView mylist=null;
private List<Map<String,Object>> filelist=new ArrayList<Map<String,Object>>();
private SimpleAdapter simple=null;
private ListFileThread tf=null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.setContentView(R.layout.activity_main);
this.mylist=(ListView)super.findViewById(R.id.mylist);
File file=new File(java.io.File.separator);//从根目录下开始列出
this.tf=new ListFileThread();
tf.execute(file);
this.mylist.setOnItemClickListener(new OnItemClickListenerlmpl());
}
private class OnItemClickListenerlmpl implements OnItemClickListener{
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
File currFile=(File)MainActivity.this.filelist.get(arg2).get("name");
if(currFile.isDirectory()){
MainActivity.this.filelist=new ArrayList<Map<String,Object>>();
ListFileThread tft=new ListFileThread();
tft.execute(currFile);
}
}
}
private class ListFileThread extends AsyncTask<File, File, String>{
@Override
protected void onProgressUpdate(File... values) {
Map<String,Object> map=new HashMap<String, Object>();
if(values[0].isDirectory()){//如果是目录
map.put("img", R.drawable.folder_close);//设置文件夹图标
}else{
map.put("img", R.drawable.file);//设置文件图标
}
map.put("name", values[0]);
MainActivity.this.filelist.add(map);
MainActivity.this.simple=new SimpleAdapter(
MainActivity.this,
MainActivity.this.filelist,
R.layout.file_out,
new String[]{"img","name"},
new int[]{R.id.img,R.id.name});
MainActivity.this.mylist.setAdapter(MainActivity.this.simple);
}
@Override
protected String doInBackground(File... params) {
if(!params[0].getPath().equals(java.io.File.separator)){//不是根目录
Map<String,Object> map=new HashMap<String, Object>();
map.put("img", R.drawable.folder_open);
map.put("name", params[0].getParentFile());
MainActivity.this.filelist.add(map);
}
if(params[0].isDirectory()){//路径是目录
File tempFile[]=params[0].listFiles();
if(tempFile!=null){
for (int i = 0; i < tempFile.length; i++) {
this.publishProgress(tempFile[i]);
}
}
}
return "执行完毕";
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
界面配置文件Activity_mail.xml
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ListView
android:id="@+id/mylist"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
表格布局file_out.xml
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TableRow>
<ImageView
android:id="@+id/img"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/name"
android:layout_width="180px"
android:layout_height="wrap_content"/>
</TableRow>
</TableLayout>
版权声明:本文为博主原创文章,未经博主允许不得转载。
android用异步操作AsyncTask编写文件查看器的更多相关文章
- WPF 模仿 UltraEdit 文件查看器系列一 用户控件
WPF 模仿 UltraEdit 文件查看器系列一 用户控件 运行环境:Win10 x64, NetFrameWork 4.8, 作者:乌龙哈里,日期:2019-05-10 章节: 起步 添加用户控件 ...
- 02-IOSCore - NSFileHandle、合并文件、文件指针、文件查看器
[day0201_NSFileHandle]:文件句柄 1 NSFileHandle 文件对接器.文件句柄 常用API: - (NSData *)readDataToEndOfFile;读取数据到最后 ...
- Qt Quick综合实例之文件查看器
假设你基于Qt SDK 5.3.1来创建一个Qt Quick App项目,项目模板为你准备的main.qml文档的根元素是ApplicationWindow或Window.这次我们就以Applicat ...
- WPF 模仿 UltraEdit 文件查看器系列 开篇和导读
WPF 模仿 UltraEdit 文件查看器系列 开篇和导读 运行环境:Win10 x64, NetFrameWork 4.8, 作者:乌龙哈里,日期:2019-05-10 学 .Net FrameW ...
- android 网络_网络源码查看器
xml设计 <?xml version="1.0"?> -<LinearLayout tools:context=".MainActivity" ...
- Android仿微信朋友圈图片查看器
转载请注明出处:http://blog.csdn.net/allen315410/article/details/40264551 看博文之前,希望大家先打开自己的微信点到朋友圈中去,细致观察是不是发 ...
- 安装XPS文件查看器的方法
方法1https://jingyan.baidu.com/article/ca2d939d6eb0eeeb6c31cecb.html 方法2 Win10需要使用这种方式,因为Windows 10:版本 ...
- PHP 简易文件查看器
超简易服务器端文件查询器 代码如下: <?php // 系统入口 date_default_timezone_set("PRC"); error_reporting(E_AL ...
- 5.1、Android Studio用Logcat编写和查看日志
Android Studio在Android Monitor中包含了一个logcat的tab,可以打印系统事件,比如垃圾回收发生时,实时打印应用消息. 为了显示需要的信息,你可以创建过滤器,更改需要显 ...
随机推荐
- MongoDB 快速入门--中级
索引 ensureIndex 用来创建索引,需要注意的就是一个集合最多也就64个索引 如果没加所有就是表扫表,速度很慢, 当然如果索引的键有多个,就必须考虑顺序 拓展索引 同样的也可以为内嵌文档 建立 ...
- 基于EF的数据外键关联查询
现在很多ORM不自带外键关联的实体查询,比如我查询用户,用时将关联的角色信息查询出来,那么就要进行2次查询,很麻烦.而我现在要做的就是基于EF的外键关联查询.很方便的. 首先,创建基础查询的BaseS ...
- 窗体 dialog 弹出时动画效果
1.先创建 anim中的 xml 动画文件 <?xml version="1.0" encoding="utf-8"? > <set x ...
- 【Android 应用开发】Android 开发环境下载地址 -- 百度网盘 adt-bundle android-studio sdk adt 下载
19af543b068bdb7f27787c2bc69aba7f Additional Download (32-, 64-bit) Package r10 STL debug info androi ...
- c++中的强制转换static_cast、dynamic_cast、reinterpret_cast的不同用法儿
c++中的强制转换static_cast.dynamic_cast.reinterpret_cast的不同用法儿 虽然const_cast是用来去除变量的const限定,但是static_cast ...
- XtraBackup原理5
http://www.cnblogs.com/gomysql/p/3650645.html xtrabackup是Percona公司CTO Vadim参与开发的一款基于InnoDB的在线热备工具,具有 ...
- Java_Utils框架
Spring Utils https://github.com/tangyanbo/springmore
- HBase Error: connection object not serializable
HBase Error: connection object not serializable 想在spark driver程序中连接HBase数据库,并将数据插入到HBase,但是在spark集群提 ...
- Asp.Net 之 网页快照
此文做法不是 Control.DrawToBitmap ,而是直接QueryInterface 浏览器Com对象的 IViewObject 接口,用它实现的Draw方法,画到图像上. 首先,定义IVi ...
- 刚安装完jdk和eclipse需要配置什么?
还需要配置环境变量,你还要下载 apache-tomcat WEB服务器,也就是说 系统能找到你的 服务器,具体配置:(在windows桌面上右击“我的电脑” —> “属性” —> “高级 ...