快速搜索手机上某文件
2017-08-18 11:27
141 查看
public class SearchFileActivity extends Activity{ List<String> list=new ArrayList<>(); private ArrayAdapter<String> adapter; private List<String> pathlist=new ArrayList<>(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.searchfile); EditText search_edit=findViewById(R.id.search_edit); ListView search_list=findViewById(R.id.search_list); adapter = new ArrayAdapter(this, android.R.layout.simple_expandable_list_item_1, list); search_list.setAdapter(adapter); search_edit.addTextChangedListener(new TextWatcher() { @Override public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) { } @Override public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) { list.clear();
//默认为sd卡,但是此时搜索较为卡顿,建议制定到某文件夹内部 searchFile(charSequence.toString().trim(), "/sdcard/"); adapter.notifyDataSetChanged(); } @Override public void afterTextChanged(Editable editable) { } }); search_list.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() { @Override public boolean onItemLongClick(AdapterView<?> adapterView, View view, int i, long l) { String s = list.get(i); //s为选定的文件夹路径 } }); } private void searchFile(String filename, String path) { File[] files = null; try { files = new File(path).listFiles(); } catch (Exception e) { files = null; Toast.makeText(this, "搜索", Toast.LENGTH_SHORT).show(); return; } for (File file : files) { if (file.isDirectory() && file.listFiles() != null) { searchFile(filename, file.getPath()); } else if (file.isFile()) { if (filename == null || filename.isEmpty()) { list.add(file.getPath()); } else { String name = file.getName(); if (name.indexOf(filename) != -1) { list.add(file.getPath()); } } } } } }
布局文件
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical" android:layout_width="match_parent"android:layout_height="match_parent"android:padding="30dp"><EditTextandroid:id="@+id/search_edit"android:layout_width="match_parent"android:layout_height="wrap_content" /><ListViewandroid:id="@+id/search_list"android:layout_width="match_parent"android:layout_height="match_parent"></ListView></LinearLayout>
相关文章推荐
- android:快速搜索手机中文件
- 【Android每日一讲】2012.11.28 快速地搜索手机文件引擎 - Java IO的应用
- Android--快速地搜索手机文件引擎小程序(File I/O)
- [置顶] 【实用软件分享01】Everything快速搜索本地文件工具
- 安卓手机与Linux同步后,搜索文件内的内容
- 搜索功能吊炸天,出门在外通过Outlook手机APP快速找到那个骚气的技术宅
- everything基于Windows平台快速搜索文件
- 安卓仿手机联系人右侧快速搜索菜单自定义View
- 使用工具快速查看SQLite数据库文件(手机已Root)
- Emacs使用Projectile管理项目,可快速访问项目里任何文件,支持全项目关键词搜索
- Win7如何快速使用搜索应用让文件查找更轻松
- C# Android BackupSupport 1.3.0(快速备份文件、打开、剪切板操作,手机做宏键盘)
- AAuto - 快速搜索文件
- 分布式文件快速搜索-设计与实现(开源/并行)
- 本地文件搜索神器 --- Everything软件,快速搜索本地磁盘上的文件
- Visual Studio 中的智能搜索——快速跳转文件
- Android源码快速查找文件、搜索字符串和编译
- 运用多线程技术实现文件的快速搜索
- Java用广度优先搜索快速搜索文件
- 文件快速搜索工具-Everything的使用(转)