Activity程序

package com.example.fileasynctaskproject;
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

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    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

<?xml version="1.0" encoding="utf-8"?>
<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编写文件查看器的更多相关文章

  1. WPF 模仿 UltraEdit 文件查看器系列一 用户控件

    WPF 模仿 UltraEdit 文件查看器系列一 用户控件 运行环境:Win10 x64, NetFrameWork 4.8, 作者:乌龙哈里,日期:2019-05-10 章节: 起步 添加用户控件 ...

  2. 02-IOSCore - NSFileHandle、合并文件、文件指针、文件查看器

    [day0201_NSFileHandle]:文件句柄 1 NSFileHandle 文件对接器.文件句柄 常用API: - (NSData *)readDataToEndOfFile;读取数据到最后 ...

  3. Qt Quick综合实例之文件查看器

    假设你基于Qt SDK 5.3.1来创建一个Qt Quick App项目,项目模板为你准备的main.qml文档的根元素是ApplicationWindow或Window.这次我们就以Applicat ...

  4. WPF 模仿 UltraEdit 文件查看器系列 开篇和导读

    WPF 模仿 UltraEdit 文件查看器系列 开篇和导读 运行环境:Win10 x64, NetFrameWork 4.8, 作者:乌龙哈里,日期:2019-05-10 学 .Net FrameW ...

  5. android 网络_网络源码查看器

    xml设计 <?xml version="1.0"?> -<LinearLayout tools:context=".MainActivity" ...

  6. Android仿微信朋友圈图片查看器

    转载请注明出处:http://blog.csdn.net/allen315410/article/details/40264551 看博文之前,希望大家先打开自己的微信点到朋友圈中去,细致观察是不是发 ...

  7. 安装XPS文件查看器的方法

    方法1https://jingyan.baidu.com/article/ca2d939d6eb0eeeb6c31cecb.html 方法2 Win10需要使用这种方式,因为Windows 10:版本 ...

  8. PHP 简易文件查看器

    超简易服务器端文件查询器 代码如下: <?php // 系统入口 date_default_timezone_set("PRC"); error_reporting(E_AL ...

  9. 5.1、Android Studio用Logcat编写和查看日志

    Android Studio在Android Monitor中包含了一个logcat的tab,可以打印系统事件,比如垃圾回收发生时,实时打印应用消息. 为了显示需要的信息,你可以创建过滤器,更改需要显 ...

随机推荐

  1. SQL NULL Values

    NULL代表缺失的.未知的数据.表的列值默认是NULL.如果某个表的某个列不是NOT NULL的,那么当我们插入新纪录.更新已存在的记录时,可以不用为此列赋值,这意味着那个列保存为NULL值. NUL ...

  2. ASP.NET MVC- Area 使用

    ASP.NET MVC允许使用 Area(区域)来组织Web应用程序,每个Area代表应用程序的不同功能模块.这对于大的工程非常有用,Area 使每个功能模块都有各自的文件夹,文件夹中有自己的Cont ...

  3. MemCacheManager

    #region Fields private AreaRepository _areaRepository = new AreaRepository(); private ICacheManager ...

  4. android Handler错误,不同的包Handler

    1. import java.util.logging.Handler;这个包了会自动生成如下方法.当时还觉得和以前的不一样了,本不在意. Handler handler1= new Handler( ...

  5. Posting data to a HttpHandler greater then ~29MB gives a 404 error

    1down votefavorite 1 I am testing a HttpHandler that accepts XML. It works fine when a small amount ...

  6. hadoop namenode又一次格式化以后hbase的hmaster进程启动后立即消失

    hadoop的 namenode又一次格式化以后.重新启动hbase.发现它的hmaster进程启动后立即消失,查看一大堆日志,最后在zookeeper的日志里发现例如以下问题 Unable to r ...

  7. Covarience And ContraVariance

    using System; using System.Collections.Generic; using System.IO; namespace CovarientAndContraVarient ...

  8. placement new 操作符

    placement new操作符能够在分配内存时指定内存位置.下面的程序使用了placement new操作符和常规new操作符给对象分配内存. // placenew.cpp -- new, pla ...

  9. IntellijIdea中常用的快捷键

    快速查找类:Ctrl+N 提示:Ctrl+Space 提示:Ctrl+Shift+Space 查看documentation:Ctrl+Q 查找类.方法.变量的引用:Alt+F7 定位类.方法.变量的 ...

  10. Shell脚本的安全性

    Shell脚本给管理和使用系统都带来了巨大的便利,然后在多用户系统中,也带来了许多安全性问题.在脚本中可能存在的安全性问题如下: (1)在脚本中使用cd命令,切换到不安全的目录执行脚本. (2)在脚本 ...