20169207 2016-2017-2 《移动平台应用开发实践》第十四周作业

这次在原有基础上增添了许多功能,包括删除操作,重命名,复制和查找。
在adapter中直接执行删除操作:


/** 
 * 删除 
 */  
private void doRemove() {  
    final File file = filedata.get(position);  
     judgeAlertDialog(context, "提醒", "你确认删除" + file.getName() + "吗(不可逆)?", new DialogInterface.OnClickListener() {  
         @Override  
         public void onClick(DialogInterface dialog, int which) {  
             deleteDir(file);  
             filedata.remove(file);  
             notifyDataSetChanged();  
             showToast(file.getName() + " 删除成功");  
         }  }, null);  
}  

1、重命名
在adapter中直接执行重命名操作:



private void doRename() {  
    showToast("重命名" + position);  
    RenameFileDialog dialog = new RenameFileDialog(context, filedata, position);  
    dialog.setOnFileRenameListener(new RenameFileDialog.OnFileRenameListener() {  
        @Override  
        public void onFileRenamed(boolean success) {  
            String message = null;  
            if (filedata.get(position).isFile()) {  
                message = "文件";  
            } else {  
                message = "文件夹";  
            }  
            if (success) {  
                message += "重命名成功";  
            } else {  
                message += "重命名失败";  
  
            }  
            showToast(message);  
        }  
    });  
    dialog.show();  
    setfiledata(filedata);  
}  

2、复制文件:

如下图所示:
使用了简单的回调执行复制操作,在FileAdapter定义复制接口,然后在MainActivity中实现接口,并通过setListener将自身实例传入FileAdapter,从而实现在FileAdapter中执行复制操作。
MainActivity中的粘贴操作实现如下:

 
private void doPaste() {  
    File newFile = new File(getPathString()+"/"+watingCopyFile.getName());  
    if (watingCopyFile.equals(null)) {  
        Snackbar.make(findViewById(R.id.main_view), "当前粘贴板为空,不能粘贴", Snackbar.LENGTH_SHORT).show();  
    } else {  
        if (watingCopyFile.isFile()&&watingCopyFile.exists()){  
            try {  
                FileInputStream fis = new FileInputStream(watingCopyFile);  
                FileOutputStream fos = new FileOutputStream(newFile);  
                int len = -1;  
                long contentSize = watingCopyFile.length();  
                long readed = 0;  
                byte[] buff = new byte[8192];  
                while ((len=fis.read(buff))!=-1){  
                    //写文件  
                    fos.write(buff,0,len);  
                    readed+=len;  
                    //发布进度  
                }  
                fos.flush();  
                fis.close();  
                fos.close();  
            } catch (IOException e) {  
                e.printStackTrace();  
            } finally {  
            }  
        }  
        if (newFile.exists()) {  
            Toast.makeText(MainActivity.this,"复制" + newFile.getName() + "成功",Toast.LENGTH_SHORT).show();  
            fileAdapter.notifyDataSetChanged();  
        }  
    }  
}  

3: 查(对当前路径下的递归查询).
实现原理:


data = new ArrayList<>();  
searchfilemap = new HashMap<>();  
  searchByPath(path);  
if (searchfilemap.size() > 0) {  
    //取出map中数据,赋值给data  
    Object[] list = searchfilemap.entrySet().toArray();  
    for (int i = 0; i < searchfilemap.size(); i++) {  
        data.add(new File(list[i].toString()));  
    }  
}  

通过递归查找本地所有文件,寻找匹配输入字符query的文件名的文件,并全部显示在listview上。
递归方法:
 



private void searchByPath(String path) {  
    File[] files = new File(path).listFiles();  
    filenum += files.length;  
    publishProgress(filenum);  
    for (int i = 0; i < files.length; i++) {  
        File f = files[i];  
        if (f.isDirectory()) {  
            searchByPath(path + "/" + f.getName());  
        } else {  
            if (f.getName().contains(query)) {  
                searchfilemap.put(files[i], files[i].getName());  
            }  
        }  
    }  
}  


4:排(对显示在listView中的文件按时间,大小或文件名排序)
实现方法使用杨羿的方式,通过对文件种类进行分类比较实现排序:
首先需要一个格式工厂类:



 
public class FileSortFactory {  
    public static final int SORT_BY_FOLDER_AND_NAME = 1;  
    public static final int SORT_BY_FOLDER_REVERSE_AND_NAME = 2;  
    public static final int SORT_BY_FOLDER_AND_NAME_REVERSE = 3;  
    public static final int SORT_BY_FOLDER_REVERSE_AND_NAME_REVERSE = 4;  
    public static final int SORT_BY_FOLDER_AND_SIZE = 5;  
    public static final int SORT_BY_FOLDER_REVERSE_AND_SIZE = 6;  
    public static final int SORT_BY_FOLDER_AND_SIZE_REVERSE = 7;  
    public static final int SORT_BY_FOLDER_REVERSE_AND_SIZE_REVERSE = 8;  
    public static final int SORT_BY_FOLDER_AND_TIME = 9;  
    public static final int SORT_BY_FOLDER_REVERSE_AND_TIME = 10;  
    public static final int SORT_BY_FOLDER_AND_TIME_REVERSE = 11;  
    public static final int SORT_BY_FOLDER_REVERSE_AND_TIME_REVERSE = 12;  
  
    public static Comparator getWebFileQueryMethod(  
            int method) {  
        switch (method) {  
            case SORT_BY_FOLDER_AND_NAME:  
                return new SortByFolderAndName(true, true);  
            case SORT_BY_FOLDER_REVERSE_AND_NAME:  
                return new SortByFolderAndName(false, true);  
            case SORT_BY_FOLDER_AND_NAME_REVERSE:  
                return new SortByFolderAndName(true, false);  
            case SORT_BY_FOLDER_REVERSE_AND_NAME_REVERSE:  
                return new SortByFolderAndName(false, false);  
            case SORT_BY_FOLDER_AND_SIZE:  
                return new SortByFolderAndSize(true, true);  
            case SORT_BY_FOLDER_REVERSE_AND_SIZE:  
                return new SortByFolderAndSize(false, true);  
            case SORT_BY_FOLDER_AND_SIZE_REVERSE:  
                return new SortByFolderAndSize(true, false);  
            case SORT_BY_FOLDER_REVERSE_AND_SIZE_REVERSE:  
                return new SortByFolderAndSize(false, false);  
            case SORT_BY_FOLDER_AND_TIME:  
                return new SortByFolderAndTime(true, true);  
            case SORT_BY_FOLDER_REVERSE_AND_TIME:  
                return new SortByFolderAndTime(false, true);  
            case SORT_BY_FOLDER_AND_TIME_REVERSE:  
                return new SortByFolderAndTime(true, false);  
            case SORT_BY_FOLDER_REVERSE_AND_TIME_REVERSE:  
                return new SortByFolderAndTime(false, false);  
            default:  
                break;  
        }  
        return null;  
  
    }  
}  

之后再对应的具体排序方法类(这里只列出一种):


 
public class SortByFolderAndName implements Comparator {  
  
    boolean first;  
    boolean second;  
  
    public SortByFolderAndName(boolean first,boolean second) {  
        this.first = first;  
        this.second = second;  
    }  
  
    @Override  
    public int compare(File lhs, File rhs) {  
        if (first) {  
            if (!lhs.isFile() && rhs.isFile()) {  
                return -1;  
            }  
            if (lhs.isFile() && !rhs.isFile()) {  
                return 1;  
            }  
        } else {  
            if (!lhs.isFile() && rhs.isFile()) {  
                return 1;  
            }  
            if (lhs.isFile() && !rhs.isFile()) {  
                return -1;  
            }  
        }  
  
        if (second) {  
            if (!(lhs.isFile() ^ rhs.isFile())) {  
                return lhs.getName().compareTo(rhs.getName());  
            }  
        } else {  
            if (!(lhs.isFile() ^ rhs.isFile())) {  
                return -lhs.getName().compareTo(rhs.getName());  
            }  
        }  
        return 0;  
    }  
}  

最后,在Adapter中通过排序方法实现对所有文件的排序:


private void sort() {  
    Collections.sort(this.filedata, FileSortFactory.getWebFileQueryMethod(sortWay));  
}  

原文地址:https://www.cnblogs.com/littletang/p/6935305.html