zoukankan      html  css  js  c++  java
  • 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>

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

  • 相关阅读:
    选择排序遇到的引用和传值问题记录
    The web application [ROOT] appears to have started a thread named [spring.cloud.inetutils] but has failed to stop it. This is very likely to create a memory leak. Stack trace of thread:
    IDEA中实用的插件
    Column 'status' specified twice
    Error updating database. Cause: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown column 'dataType' in 'field list'
    You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'where id = 2' at line 8
    Missing URI template variable 'id' for method parameter of type long
    值传递和引用传递的区别
    SpringBoot项目与数据库交互,访问http://localhost:8888/admin/userInfo时,报org.springframework.dao.EmptyResultDataAccessException: Incorrect result size: expected 1, actual 0
    SpringBoot项目启动时报错:org.apache.catalina.LifecycleException: Protocol handler start failed
  • 原文地址:https://www.cnblogs.com/liyuanjinglyj/p/4656597.html
Copyright © 2011-2022 走看看