• 异步处理(列出所有文件)




    不能操作系统的文件,因为没有root权限。

      不是单独是使用循环完成,而是单独的使用一个异步处理器完成。

    准备三张图片,名字分别为:file、folder_close和folder_open。

    在main.xml中:

    <LinearLayout

        xmlns:android="http://schemas.android.com/apk/res/android"

        android:layout_width="fill_parent"

        android:layout_height="fill_parent"

        android:background="#000000"

        android:orientation="vertical"

        android:gravity="center_horizontal">

      <ListView

          android:id="@+id/list"

          android:layout_gravity="center_horizontal"

          android:layout_width="fill_parent"

          android:layout_height="wrap_content"/>

         

    </LinearLayout>

    新建布局文件file_list.xml:

    <TableLayout

        xmlns:android="http://schemas.android.com/apk/res/android"

        android:layout_width="fill_parent"

        android:layout_height="fill_parent"

        android:background="#000000"

        android:orientation="vertical">

        <TableRow

            android:gravity="center_vertical">

            <ImageView

                android:id="@+id/img"

               android:layout_margin="8dp"

                android:layout_width="wrap_content"

                android:layout_height="wrap_content"/>

            <TextView

                android:id="@+id/name"

                android:layout_width="wrap_content"

                android:layout_height="wrap_content"

                android:textColor="#ffffff"/>

        </TableRow>

    </TableLayout>

    在MyAsyncTaskListFileDemo.java中:

    package com.li.asynctasklistfiledemo;

    import java.io.File;

    import java.util.ArrayList;

    import java.util.HashMap;

    import java.util.List;

    import java.util.Map;

    import android.app.Activity;

    import android.os.AsyncTask;

    import android.os.Bundle;

    import android.view.View;

    import android.widget.AdapterView;

    import android.widget.AdapterView.OnItemClickListener;

    import android.widget.ListView;

    import android.widget.SimpleAdapter;

    public class MyAsyncTaskListFileDemo extends Activity {

      private List<Map<String,Object>> allFileItems = new ArrayList<Map<String,Object>>() ;

      private SimpleAdapter simple = null ;

      private ListView list = null ;

      private ListFileThread ft = null ;

      @Override

      public void onCreate(Bundle savedInstanceState) {

         super.onCreate(savedInstanceState);

         super.setContentView(R.layout.main);

         this.list = (ListView) super.findViewById(R.id.list) ;

         File filePath = new File(java.io.File.separator); // 从根目录下开始列出

         this.list.setOnItemClickListener(new OnItemClickListenerImpl()) ;

         this.ft = new ListFileThread() ;

         this.ft.execute(filePath) ;

      }

      private class OnItemClickListenerImpl implements OnItemClickListener{

         public void onItemClick(AdapterView<?> parent, View view, int position,

             long id) {

           File currFile = (File) MyAsyncTaskListFileDemo.this.allFileItems

                .get(position).get("name");

           if (currFile.isDirectory()) { // 当前是一个目录

             MyAsyncTaskListFileDemo.this.allFileItems = new ArrayList<Map<String,Object>>() ;

             MyAsyncTaskListFileDemo.this.ft = new ListFileThread() ;

             MyAsyncTaskListFileDemo.this.ft.execute(currFile) ;

           }

         }

        

      }

      private class ListFileThread extends AsyncTask<File, File, String> {

        

         @Override

         protected void onProgressUpdate(File... values) {

           Map<String,Object> fileItem = new HashMap<String,Object>() ;  // 表示可以返回

           if (values[0].isDirectory()) {

             fileItem.put("img", R.drawable.folder_close); // 文件夹

           } else { // 是文件

             fileItem.put("img",R.drawable.file) ;

           }

           fileItem.put("name", values[0]) ;

           MyAsyncTaskListFileDemo.this.allFileItems.add(fileItem) ;

           MyAsyncTaskListFileDemo.this.simple = new SimpleAdapter(

                MyAsyncTaskListFileDemo.this,

                MyAsyncTaskListFileDemo.this.allFileItems,

                R.layout.file_list, new String[] { "img", "name" },

                new int[] { R.id.img, R.id.name });

           MyAsyncTaskListFileDemo.this.list

                .setAdapter(MyAsyncTaskListFileDemo.this.simple);

         }

         @Override

         protected String doInBackground(File... params) {

          if (!params[0].getPath().equals(java.io.File.separator)) { // 不是根目录

             Map<String,Object> fileItem = new HashMap<String,Object>() ;  // 表示可以返回

             fileItem.put("img",R.drawable.folder_open) ; // 可以返回

             fileItem.put("name", params[0].getParentFile()) ;

             MyAsyncTaskListFileDemo.this.allFileItems.add(fileItem) ;

           }

           if (params[0].isDirectory()) { // 是文件夹

             File tempFile [] = params[0].listFiles() ;

             if(tempFile != null) {

                for (int x = 0; x < tempFile.length; x++) {

                  this.publishProgress(tempFile[x]) ;

                }

             }

           }

           return "文件已列出";

         }

      }

    }

  • 相关阅读:
    wtforms 钩子函数
    sqlalchemy 单表增删改查
    sqlalchemy 数据库操作
    flask wtforms组件
    python----------文件操作
    编码 编码的转化
    集合、深浅copy
    字典
    sort 、sorted、range、join方法 数字的正序、倒叙、翻转
    for循环
  • 原文地址:https://www.cnblogs.com/suncoolcat/p/3313164.html
Copyright © 2020-2023  润新知