一个搜索文件的APP(2)-搜索功能的实现

今天把搜索的代码放上去了。效果图如下。



MainActivity.java

[java]  view plain  copy
  1. package com.stk.afinder;  
  2.   
  3. import android.os.Bundle;  
  4. import android.app.Activity;  
  5. import android.view.Menu;  
  6. import android.widget.Button;  
  7. import android.view.View;  
  8. import android.widget.ListView;  
  9. import android.widget.ArrayAdapter;  
  10. import android.widget.Toast;  
  11. import android.widget.EditText;  
  12. import java.util.ArrayList;  
  13. import java.io.File;  
  14.   
  15. public class MainActivity extends Activity {  
  16.   
  17.     static private ArrayList<String> search_result = new ArrayList<String>();  
  18.   
  19.     @Override  
  20.     protected void onCreate(Bundle savedInstanceState) {  
  21.         super.onCreate(savedInstanceState);  
  22.         setContentView(R.layout.activity_main);  
  23.   
  24.         ListView list = (ListView) findViewById(R.id.result_list);  
  25.         list.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_expandable_list_item_1,search_result));  
  26.   
  27.         Button searchBn = (Button) findViewById(R.id.search_bn);  
  28.         searchBn.setOnClickListener(new View.OnClickListener() {  
  29.             public void onClick(View v) {  
  30.                 if (search_result != null) {  
  31.                     search_result.clear();  
  32.                 }  
  33.   
  34.                 EditText et = (EditText) findViewById(R.id.file_name_edit);  
  35.                 MainActivity.this.searchFile(et.getText().toString().trim(), "/sdcard");  
  36.                 ((ArrayAdapter)((ListView)MainActivity.this.findViewById(R.id.result_list)).getAdapter()).notifyDataSetChanged();  
  37.             }  
  38.         });  
  39.     }  
  40.   
  41.   
  42.     @Override  
  43.     public boolean onCreateOptionsMenu(Menu menu) {  
  44.         // Inflate the menu; this adds items to the action bar if it is present.  
  45.         getMenuInflater().inflate(R.menu.main, menu);  
  46.         return true;  
  47.     }  
  48.   
  49.     private void searchFile(String filename, String path) {  
  50.         File[] files = null;  
  51.         try {  
  52.             files = new File(path).listFiles();  
  53.         } catch (Exception e) {  
  54.             files = null;  
  55.             Toast.makeText(this, getString(R.string.open_file_err), Toast.LENGTH_SHORT).show();  
  56.             return;  
  57.         }  
  58.   
  59.         for (File file : files) {  
  60.             if (file.isDirectory() && file.listFiles() != null) {  
  61.                 searchFile(filename, file.getPath());  
  62.             } else if (file.isFile()) {                  
  63.                 if (filename == null || filename.isEmpty()) {  
  64.                     search_result.add(file.getPath());  
  65.                 } else {  
  66.                     String name = file.getName();  
  67.                     if (name.indexOf(filename) != -1) {  
  68.                         search_result.add(file.getPath());  
  69.                     }  
  70.                 }  
  71.             }  
  72.         }  
  73.     }  
  74. }  

其中实现搜索的代码是 private void searchFile(String filename, String path) 这个函数。

为了简单起见目前设置搜索的路径代码中硬编码为 “/sdcard”,预先在手机 /sdcard/download/ 目录下创建了几个文件。

小技巧:通过android sdk自带的工具adb操作虚拟机中的文件。

在命令行中进入android sdk安装目录中的 platform-tools 目录。

使用命令 ./adb shell 连接到 shell。android手机操作系统底层是基于Linux的,所以进入shell后基本的linux命令都可以用。

比如 ls 查看文件,cd 切换目录,touch 创建文件。完成后查看结果如下:

# cd Download
# ls
a.txt
ac.txt
b.txt
c.txt


与之前的MainActivity类代码相比,除了增加了一个private void searchFile(String filename, String path) 函数外,onCreate函数中也增加了部分内容。

1、增加了显示结果的ListView组件的处理。

[java]  view plain  copy
  1. <pre name="code" class="java">        ListView list = (ListView) findViewById(R.id.result_list); //获取组件  
  2.         list.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_expandable_list_item_1,search_result)); //绑定适配器</pre>  

我们的查找结果是一个列表,包含了所有匹配条件的文件的完整路径,我们使用一个字符串数组列表保存结果。

[java]  view plain  copy
  1. static private ArrayList<String> search_result = new ArrayList<String>();  

要使ListView与数据绑定,显示数据并且当数据发生改变时能够刷新,必须给ListView绑定一个适配器。

我们这里使用了最简单的ArrayAdapter,系统自带的一个适配器。这是最简单的一个适配器。


2、增加了搜索按钮的点击事件处理。

[java]  view plain  copy
  1. Button searchBn = (Button) findViewById(R.id.search_bn);  
  2.         searchBn.setOnClickListener(new View.OnClickListener() {  
  3.             public void onClick(View v) {  
  4.                 if (search_result != null) {  
  5.                     search_result.clear(); //清除上次的结果  
  6.                 }  
  7.   
  8.                 EditText et = (EditText) findViewById(R.id.file_name_edit); // 获取文件名的组件  
  9.                 MainActivity.this.searchFile(et.getText().toString().trim(), "/sdcard"); //调用搜索函数  
  10.                 ((ArrayAdapter)((ListView)MainActivity.this.findViewById(R.id.result_list)).getAdapter()).notifyDataSetChanged(); // 搜索完成后调用适配器的 notifyDataChanged 函数通知适配器数据源发生了改变。  
  11.             }  
  12.         });  

3、提示

在searchFile函数中有如下处理

[java]  view plain  copy
  1. try {  
  2.             files = new File(path).listFiles();  
  3.         } catch (Exception e) {  
  4.             files = null;  
  5.             Toast.makeText(this, getString(R.string.open_file_err), Toast.LENGTH_SHORT).show();  
  6.             return;  
  7.         }  

open_file_err 是我们在String.xml资源文件中新增的一个错误提示字符串

<string name="open_file_err">打开搜索目录失败,请检查设置是否正确。</string>

Toast类的makeText函数声明如下:

[java]  view plain  copy
  1. /** 
  2.  * Make a standard toast that just contains a text view. 
  3.  * 
  4.  * @param context  The context to use.  Usually your {@link android.app.Application} 
  5.  *                 or {@link android.app.Activity} object. 
  6.  * @param text     The text to show.  Can be formatted text. 
  7.  * @param duration How long to display the message.  Either {@link #LENGTH_SHORT} or 
  8.  *                 {@link #LENGTH_LONG} 
  9.  * 
  10.  */  
  11. public static Toast makeText(Context context, CharSequence text, int duration)  
如果用户没有输入任何字符串而直接点击按钮,将会列出 /sdcard 目录下的所有文件。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值