• android用异步操作AsyncTask编写文件查看器


    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>

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

  • 相关阅读:
    解决 “页面中文字增多,字号突然变大” 的问题
    Seajs的用法
    clientWidth、clientHeight、offsetWidth、offsetHeight以及scrollWidth、scrollHeight
    页面中文字增多,字号会突然变大
    flex的使用实例
    移动端弹窗滚动时window窗体也一起滚动的解决办法
    display:inline-block带来的问题及解决办法
    sql 整理积累
    T-SQL建索引
    php json_encode在CI框架中的使用细节
  • 原文地址:https://www.cnblogs.com/liyuanjinglyj/p/4656597.html
Copyright © 2020-2023  润新知