//輸入分數時精確查找,而輸入其餘時模糊查找 public class MainActivity extends Activity { //訪問其餘app的ContentProvider暴露出來的authorities示例 private String student = "content://com.example.providercustomUtils.mycontentprovider/studentinfo";// 就想訪問數據庫字段,模糊查詢 // student_score訪問的是數字,被操做的應用那裏要寫一個匹配器,/#個是數字的標誌 private String student_score = "content://com.example.providercustomUtils.mycontentprovider/studentinfo_id"; private ListView listview; private SearchView searchView; private SimpleAdapter adapter; private List<Map<String, String>> list; private TextView empty; private ContentResolver resolver; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); searchView = (SearchView) this.findViewById(R.id.searchView_main); listview = (ListView) this.findViewById(R.id.listView_main_wordslist); empty = (TextView) this.findViewById(R.id.text_main_emptyinfo); resolver = getContentResolver(); listview.setEmptyView(empty); list = getDataFillListView(null);// 查詢文本框傳入null,表明剛開始的時候加載所有數據 adapter = new SimpleAdapter(this, list, R.layout.item_listview, new String[] { "sname", "score" }, new int[] { R.id.text_item_name, R.id.text_item_score }); listview.setAdapter(adapter); searchView.setOnQueryTextListener(new OnQueryTextListener() { @Override public boolean onQueryTextSubmit(String query) { return false; } @Override // 每次搜索文本框內容改變就會觸發 //newText:爲查詢條件 public boolean onQueryTextChange(String newText) { fillListView(newText); return true;// 這裏要b把false改成true } }); } public void fillListView(String newText) { // 先把原來的數據清空 list.clear(); List<Map<String, String>> newlist = getDataFillListView(newText); list.addAll(newlist); adapter.notifyDataSetChanged(); } // keywords是搜索條件 public List<Map<String, String>> getDataFillListView(String keyword) { Cursor cursor = null; if (keyword == null) {// 空默認查找全部數據 cursor = resolver.query(Uri.parse(student), null, null, null, "score desc"); } else { if (isNumber(keyword)) {// 判斷條件是不是數字 // ContentUris.withAppendedId(contentUri, id),將一個Uri在最後加進id或數字 cursor = resolver.query(ContentUris.withAppendedId( Uri.parse(student_score), Long.parseLong(keyword)), null, null, null, null); } else { cursor = resolver.query( ContentUris.withAppendedId(Uri.parse(student), Long.parseLong(keyword)), null, null, null, null); } } return cursorToList(cursor); } public List<Map<String, String>> cursorToList(Cursor cursor) { List<Map<String, String>> list = new ArrayList<Map<String, String>>(); while (cursor.moveToNext()) {// 循環:行 Map<String, String> map = new HashMap<String, String>(); // key:是columName列名 for (int i = 0; i < cursor.getColumnCount(); i++) {// 循環:列 map.put(cursor.getColumnName(i), cursor.getString(i)); } list.add(map); } cursor.close(); return list; } public boolean isNumber(String keyword) { // 使用正則表達式判斷數字 Pattern pattern = Pattern.compile("[0-9]+");// 表示至少一個數字 return pattern.matcher(keyword).matches(); // 看是否匹配傳入的參數再matches()才返回boolean值 } } //主佈局文件 <SearchView android:id="@+id/searchView_main" android:layout_width="wrap_content" android:layout_height="wrap_content" android:iconifiedByDefault="false" android:queryHint="請輸入關鍵字查詢"> </SearchView> <ListView android:id="@+id/listView_main_wordslist" android:layout_width="match_parent" android:layout_height="match_parent" > </ListView> <TextView android:id="@+id/text_main_emptyinfo" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="暫無數據。。。" /> //listview的自定義佈局 <TextView android:id="@+id/text_item_name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="#f00" android:text="" /> <TextView android:id="@+id/text_item_score" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="" />