接上一篇,今天把搜索的代碼放上去了。效果圖如下。
MainActivity.java
package com.stk.afinder;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.Button;
import android.view.View;
import android.widget.ListView;
import android.widget.ArrayAdapter;
import android.widget.Toast;
import android.widget.EditText;
import java.util.ArrayList;
import java.io.File;
public class MainActivity extends Activity {
static private ArrayList<String> search_result = new ArrayList<String>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView list = (ListView) findViewById(R.id.result_list);
list.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_expandable_list_item_1,search_result));
Button searchBn = (Button) findViewById(R.id.search_bn);
searchBn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (search_result != null) {
search_result.clear();
}
EditText et = (EditText) findViewById(R.id.file_name_edit);
MainActivity.this.searchFile(et.getText().toString().trim(), "/sdcard");
((ArrayAdapter)((ListView)MainActivity.this.findViewById(R.id.result_list)).getAdapter()).notifyDataSetChanged();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
private void searchFile(String filename, String path) {
File[] files = null;
try {
files = new File(path).listFiles();
} catch (Exception e) {
files = null;
Toast.makeText(this, getString(R.string.open_file_err), 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()) {
search_result.add(file.getPath());
} else {
String name = file.getName();
if (name.indexOf(filename) != -1) {
search_result.add(file.getPath());
}
}
}
}
}
}
其中實現搜索的代碼是 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組件的處理。
ListView list = (ListView) findViewById(R.id.result_list); //獲取組件 list.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_expandable_list_item_1,search_result)); //綁定適配器
我們的查找結果是一個列表,包含了所有匹配條件的文件的完整路徑,我們使用一個字符串數組列表保存結果。
static private ArrayList<String> search_result = new ArrayList<String>();
要使ListView與數據綁定,顯示數據並且當數據發生改變時能夠刷新,必須給ListView綁定一個適配器。
我們這裏使用了最簡單的ArrayAdapter,系統自帶的一個適配器。這是最簡單的一個適配器。
2、增加了搜索按鈕的點擊事件處理。
Button searchBn = (Button) findViewById(R.id.search_bn);
searchBn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (search_result != null) {
search_result.clear(); //清除上次的結果
}
EditText et = (EditText) findViewById(R.id.file_name_edit); // 獲取文件名的組件
MainActivity.this.searchFile(et.getText().toString().trim(), "/sdcard"); //調用搜索函數
((ArrayAdapter)((ListView)MainActivity.this.findViewById(R.id.result_list)).getAdapter()).notifyDataSetChanged(); // 搜索完成後調用適配器的 notifyDataChanged 函數通知適配器數據源發生了改變。
}
});
3、提示
在searchFile函數中有如下處理
try {
files = new File(path).listFiles();
} catch (Exception e) {
files = null;
Toast.makeText(this, getString(R.string.open_file_err), Toast.LENGTH_SHORT).show();
return;
}
open_file_err 是我們在String.xml資源文件中新增的一個錯誤提示字符串
<string name="open_file_err">打開搜索目錄失敗,請檢查設置是否正確。</string>
Toast類的makeText函數聲明如下:
/**
* Make a standard toast that just contains a text view.
*
* @param context The context to use. Usually your {@link android.app.Application}
* or {@link android.app.Activity} object.
* @param text The text to show. Can be formatted text.
* @param duration How long to display the message. Either {@link #LENGTH_SHORT} or
* {@link #LENGTH_LONG}
*
*/
public static Toast makeText(Context context, CharSequence text, int duration)
如果用戶沒有輸入任何字符串而直接點擊按鈕,將會列出 /sdcard 目錄下的所有文件。
寫東西好麻煩啊 :( ,比寫代碼麻煩多了。
總是寫的簡單了,好多東西沒有寫出來。