博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Android手机文件管理
阅读量:5748 次
发布时间:2019-06-18

本文共 9021 字,大约阅读时间需要 30 分钟。

MyAdapter.java
1 package com.testview; 2  3 import java.util.List; 4 import java.util.Map; 5  6 import android.content.Context; 7 import android.graphics.Color; 8 import android.view.LayoutInflater; 9 import android.view.View;10 import android.view.ViewGroup;11 import android.widget.BaseAdapter;12 import android.widget.TextView;13 14 public class MyAdapter extends BaseAdapter {15     private LayoutInflater mInflater;16     private List
>mData ;17 private int selectItem=-1;18 private int[] layout;19 public MyAdapter(Context context, List
>mData, int[] layout) {20 this.mInflater = LayoutInflater.from(context);21 this.mData = mData;22 this.layout = layout;23 }24 public int getCount() {25 return mData.size();26 }27 public Object getItem(int arg0) {28 return mData.get(arg0);29 }30 public long getItemId(int arg0) {31 return arg0;32 }33 public View getView(int position, View convertView, ViewGroup parent) {34 ViewHolder holder = null;35 if (convertView == null) {36 holder = new ViewHolder();37 convertView = mInflater.inflate(layout[0], null);38 holder.titleText = (TextView) convertView.findViewById(layout[1]);39 holder.infoText = (TextView) convertView.findViewById(layout[2]);40 41 convertView.setTag(holder); 42 } else {43 holder = (ViewHolder) convertView.getTag();44 }45 holder.titleText.setText((String) mData.get(position).get("title"));46 holder.infoText.setText((String) mData.get(position).get("info")); 47 48 if (position == selectItem) {49 convertView.setBackgroundColor(0xaeefcc00);50 } 51 else {52 convertView.setBackgroundColor(Color.BLACK);53 }54 convertView.getBackground().setAlpha(20);//0~255透明度值55 return convertView;56 }57 public void setSelectItem(int selectItem) {58 this.selectItem = selectItem;59 }60 61 }
TestListViewActivity .java
1 package com.testview;  2   3 import java.io.File;  4 import java.util.ArrayList;  5 import java.util.Arrays;  6 import java.util.HashMap;  7 import java.util.List;  8 import java.util.Map;  9 import com.util.SDUtil; 10 import android.app.Activity; 11 import android.app.AlertDialog; 12 import android.os.Bundle; 13 import android.os.Environment; 14 import android.view.KeyEvent; 15 import android.view.View; 16 import android.view.View.OnClickListener; 17 import android.widget.AdapterView; 18 import android.widget.EditText; 19 import android.widget.ImageButton; 20 import android.widget.ListView; 21  22 public class TestListViewActivity extends Activity { 23     /** 24      * 数据存储 25      */ 26     private List
> mData; 27 28 /** 29 * 当前访问路径 30 */ 31 private String currentPath = "/sdcard"; 32 33 private ListView setlistViewLeft; 34 /** 35 * 自定义数据适配器 36 */ 37 private MyAdapter adapter; 38 39 /** 40 * 文件名称 41 */ 42 private String[] fileNames = null; 43 44 /** 45 * 文件路径 46 */ 47 private String[] filePaths = null; 48 49 private EditText editText; 50 private ImageButton imageBtn; 51 52 private int[] layout = { 53 R.layout.listinfo, 54 R.id.titleleftlist, 55 R.id.infoleftlist 56 }; 57 58 @Override 59 public void onCreate(Bundle savedInstanceState) { 60 super.onCreate(savedInstanceState); 61 setContentView(R.layout.main); 62 setTitle("文件总数:"+getSDCardMassage(currentPath)+"\n"+"当前路径:"+currentPath); 63 getSearchKey(); 64 mData = getListItems(); 65 setlistViewLeft = (ListView)findViewById(R.id.listleft); 66 adapter = new MyAdapter(this, mData, layout); 67 setlistViewLeft.setAdapter(adapter); 68 setlistViewLeft.setOnItemClickListener(mLeftListOnItemClick); 69 } 70 71 /** 72 * 初始化数据 73 */ 74 public void getSearchKey(){ 75 editText = (EditText) findViewById(R.id.entry); 76 imageBtn = (ImageButton) findViewById(R.id.btn_imageButton); 77 imageBtn.setOnClickListener(new OnClickListener() { 78 79 @Override 80 public void onClick(View v) { 81 String searchKey = editText.getText().toString(); 82 if(searchKey == null || "".equals(searchKey)){ 83 return; 84 } 85 int length = fileNames.length; 86 String[] strKeyName = {}; 87 String[] strKeyPath = {}; 88 for(int i = 0; i < length; i++){ 89 String name = fileNames[i]; 90 if(name.contains(searchKey)){ 91 strKeyName = Arrays.copyOf(strKeyName, strKeyName.length+1); 92 strKeyPath = Arrays.copyOf(strKeyPath, strKeyPath.length+1); 93 strKeyName[strKeyName.length-1] = name; 94 strKeyPath[strKeyPath.length-1] = filePaths[i]; 95 } 96 } 97 if(strKeyName.length > 0){ 98 //初始化数据 99 setSearchKeyData(strKeyName, strKeyPath);100 101 }else{102 new AlertDialog.Builder(TestListViewActivity.this)103 .setTitle("消息")104 .setMessage("文件不存在")105 .setPositiveButton("确定", null)106 .show();107 }108 }109 110 111 });112 }113 114 /**115 * 初始化搜索数据116 * @param strKeyName117 * @param strKeyPath118 */119 private void setSearchKeyData(String[] strKeyName, String[] strKeyPath) {120 fileNames = strKeyName;121 filePaths = strKeyPath;122 mData = getListItems();123 setTitle("文件总数:"+strKeyName.length+"\n"+"当前路径:"+currentPath);124 initAdapter();125 }126 127 /**128 * 初始化数据129 * @param path 文件路径130 */131 public void initData(String path){132 currentPath = path;133 setTitle("文件总数:"+getSDCardMassage(path)+" "+currentPath);134 mData = getListItems();135 editText.setText(null);136 initAdapter();137 }138 139 public void initAdapter(){140 setlistViewLeft = (ListView)findViewById(R.id.listleft);141 adapter = new MyAdapter(this, mData, layout);142 setlistViewLeft.setAdapter(adapter);143 setlistViewLeft.setOnItemClickListener(mLeftListOnItemClick);144 }145 146 private List
> getListItems(){147 List
> listItems = new ArrayList
>();148 for(int i=0;i
map = new HashMap
();150 map.put("title", "文件名称");151 map.put("info", fileNames[i]);152 listItems.add(map);153 }154 return listItems;155 }156 157 AdapterView.OnItemClickListener mLeftListOnItemClick = new AdapterView.OnItemClickListener() {158 159 @Override160 public void onItemClick(AdapterView
arg0, View arg1, int arg2,161 long arg3) {162 163 adapter.setSelectItem(arg2);164 adapter.notifyDataSetInvalidated();165 String v2 = mData.get(arg2).get("info").toString();166 String str = filePaths[arg2];167 if(str != null){168 //重新初始化界面169 mData.clear();170 currentPath = str;171 initData(str);172 }else{173 SDUtil.openFile(TestListViewActivity.this, currentPath+"/"+v2);174 }175 176 }177 178 };179 /**180 * 重写手机返回键事件 181 */182 public boolean onKeyDown(int keyCode, android.view.KeyEvent event) {183 if(keyCode == KeyEvent.KEYCODE_BACK ){184 back();185 return true;186 }187 return super.onKeyDown(keyCode, event);188 };189 190 /**191 * 返回数据初始化192 */193 private void back() {194 String path = new File(currentPath).getParentFile().getPath();195 if("/sdcard".equals(currentPath)){196 finish();197 return;198 }199 initData(path);200 }201 202 /**203 * 获取SD卡文件204 * @param path205 * @return206 */207 public int getSDCardMassage(String path){208 boolean flag = Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED);209 if(flag){210 if(SDUtil.isExistFiles(path)){211 File f = new File(path);212 File[] files = f.listFiles();213 int size = files.length;214 fileNames = new String[size];215 filePaths = new String[size];216 for(int i = 0; i < size; i++ ){217 File exitFile = files[i];218 if(exitFile.exists()){219 fileNames[i] = exitFile.getName();220 if(SDUtil.isExistFiles(exitFile.getAbsolutePath()))221 filePaths[i] = exitFile.getAbsolutePath();222 else 223 filePaths[i] = null;224 }225 }226 return size;227 }228 }229 return 0;230 }231 }
main.xml
1 
2
6
11
20
30
31
35
36
listinfo.xml

 界面展示如下:

转载于:https://www.cnblogs.com/FCWORLD/archive/2012/10/18/2729949.html

你可能感兴趣的文章
UIPickerView的自定义视图
查看>>
Flash pixel Bender学习笔记
查看>>
apache2.4配置多个端口对应多个目录
查看>>
vue-router
查看>>
雷林鹏分享:codeigniter框架文件上传处理
查看>>
数据库连接
查看>>
Android开发学习——自定义View
查看>>
JQ使用Append添加html文本后再删除该html文本
查看>>
B/S与C/S结构的区别
查看>>
MathType中常见的两种符号的运用
查看>>
自动填充功能关闭解决表单input框屎黄色问题
查看>>
python_控制台输出带颜色的文字方法
查看>>
Java线程Thread的状态解析以及状态转换分析 多线程中篇(七)
查看>>
[十二]JavaIO之BufferedInputStream BufferedOutputStream
查看>>
java泛型中特殊符号的含义
查看>>
一秒 解决 ERROR 1044 (42000): Access denied for user ''@'localhost' to database 'mysql 问题
查看>>
linuxan安装redis出现Newer version of jemalloc required错误
查看>>
在centos7下用http搭建配置svn服务
查看>>
PHP APP端支付宝支付
查看>>
TCP长连接的一些事儿
查看>>