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
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 26 11 3120 30 35 36
listinfo.xml
界面展示如下: