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>

版权声明:本文为博主原创文章,未经博主允许不得转载。

更多相关文章

  1. Android中子线程网络查看器与Handler消息处理器
  2. Android字体设置
  3. Android中LayoutParams的用法
  4. Android(安卓)Http请求框架一:Get 和 Post 请求
  5. android使用Intent操作拨打号码发送短信
  6. Android判断app是否打开消息通知并跳转设置
  7. 6 android 滑块和进度条
  8. Android获取SDcard目录及创建文件夹;
  9. Android动态设置View的位置和大小

随机推荐

  1. 接口 登录、注册、信息
  2. android自定义一圆角ImageView
  3. 调用摄像头拍照并显示
  4. android 读中文文本文件
  5. Android抽屉(SlidingDrawer)的实现
  6. Android中的数据存储方式
  7. Android中获取全局Context
  8. 单条-多条 折线图
  9. ViewPager无限轮播图
  10. 使用ComponentName启动Activity