Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android開發之搜索框自動提示細節MatrixCursor學習

Android開發之搜索框自動提示細節MatrixCursor學習

編輯:關於Android編程

1,在ActionBar裡面加入搜索框

在ActionBar裡面加入搜索框是現在APP應用的常用做法。要想把搜索做好,可不是那麼容易。涉及到自動提示和真正的搜索。下圖是我對一個相對完整的搜索框涉及的元素關系做了一個圖,箭頭只表示它們之間有關系,不是類圖中的繼承。

一個Activity會有一個ActionBar,默認情況下。你可以在ActionBar裡面添加菜單條目。菜單條目可以設置標題,圖標以及關聯的ActionView,這裡我們關聯了一個SearchView。SearchView和SearchableInfo關聯,和搜索服務關聯。同時為了更好的處理關於SearchView的事件響應,我們抽象了一個控制類MovieSearch。它是邏輯控制中心,有搜索完了的跳轉SearchActivity(包含SearchFregment),也有內容提示。內容提示主要是根據輸入的字進行聯想,或者根據歷史記錄或者熱門搜索。所以這裡有個SearchHistory,它又和數據表相關。

vcHL0ru49sDgTWF0cml4Q3Vyc29yo6zL+dLUo6zO0sPHz+rPuLXEwcu94tXiuPbA4LXE08O3qKGjPC9wPgo8aDI+MqOsTWF0cml4Q3Vyc29yz+q94jwvaDI+CjxwPjxicj4KPC9wPgo8cD48L3A+CrzZyOfT0NK7uPbI58/CtcTK/b7dv+Kx7b3hubkKCjx0YWJsZSBib3JkZXI9"1" cellspacing="0" cellpadding="2" width="100%"> _id name price R.drawable.ic_launcher zhangsan 39 R.drawable.ic_launcher lisi 40 R.drawable.ic_launcher wangwu 41 R.drawable.ic_launcher zhaoliu 42
現在咱們就通過MatrixCursor這個東西,來虛構出一張上面那樣的表結構,下面通過一個例子,就可以完全理解MatrixCursor這個東西 見示例代碼如下: [java] view plaincopy在CODE上查看代碼片派生到我的代碼片

  1. package com.test.matrixcursor;
  2. import android.app.ListActivity;
  3. import android.database.MatrixCursor;
  4. import android.os.Bundle;
  5. import android.support.v4.widget.SimpleCursorAdapter;
  6. import android.view.View;
  7. import android.widget.ListView;
  8. import android.widget.Toast;
  9. public class MainActivity extends ListActivity {
  10. private static final String[] COLUMN_NAME = { "_id", "name", "price" };
  11. private MatrixCursor matrixCursor;
  12. @Override
  13. protected void onCreate(Bundle savedInstanceState) {
  14. super.onCreate(savedInstanceState);
  15. createList();
  16. //createList2();
  17. }
  18. private void createList() {
  19. matrixCursor = new MatrixCursor(COLUMN_NAME, 1);
  20. startManagingCursor(matrixCursor);
  21. matrixCursor.addRow(new Object[] { R.drawable.ic_launcher, "zhangsan",
  22. 39 });
  23. matrixCursor
  24. .addRow(new Object[] { R.drawable.ic_launcher, "lisi", 40 });
  25. matrixCursor
  26. .addRow(new Object[] { R.drawable.ic_launcher, "wangwu", 41 });
  27. matrixCursor.addRow(new Object[] { R.drawable.ic_launcher, "zhaoliu",
  28. 42 });
  29. matrixCursor
  30. .addRow(new Object[] { R.drawable.ic_launcher, "sunqi", 43 });
  31. setListAdapter(new SimpleCursorAdapter(this, R.layout.activity_main,
  32. matrixCursor, COLUMN_NAME, new int[] { R.id.icon, R.id.name,
  33. R.id.price }));
  34. }
  35. private void createList2() {
  36. matrixCursor = new MatrixCursor(COLUMN_NAME, 1);
  37. startManagingCursor(matrixCursor);
  38. MatrixCursor.RowBuilder builder1 = matrixCursor.newRow();
  39. builder1.add(R.drawable.ic_launcher);
  40. builder1.add("zhangsan");
  41. builder1.add(39);
  42. MatrixCursor.RowBuilder builder2 = matrixCursor.newRow();
  43. builder2.add(R.drawable.ic_launcher);
  44. builder2.add("lisi");
  45. builder2.add(40);
  46. MatrixCursor.RowBuilder builder3 = matrixCursor.newRow();
  47. builder3.add(R.drawable.ic_launcher);
  48. builder3.add("wangwu");
  49. builder3.add(41);
  50. setListAdapter(new SimpleCursorAdapter(this, R.layout.activity_main,
  51. matrixCursor, COLUMN_NAME, new int[] { R.id.icon, R.id.name,
  52. R.id.price }));
  53. }
  54. @Override
  55. protected void onListItemClick(ListView l, View v, int position, long id) {
  56. super.onListItemClick(l, v, position, id);
  57. matrixCursor.moveToPosition(position);
  58. StringBuilder builder = new StringBuilder();
  59. builder.append("Name:")
  60. .append(matrixCursor.getString(matrixCursor
  61. .getColumnIndex("name"))).append("\n");
  62. builder.append("Price:")
  63. .append(matrixCursor.getString(matrixCursor
  64. .getColumnIndex("price"))).append("\n");
  65. Toast.makeText(getApplicationContext(), builder.toString(), 1000)
  66. .show();
  67. }
  68. }

  69. 實現步驟僅需3步,下面通過字符數組來說明: 步驟1.首先創建一個字符數組,且字符數組的值對應著表的字段,如下: String[] COLUMN_NAME = { "_id", "name", "price" }; 步驟2.利用MatrixCursor的構造方法,構造一個MatrixCursor,傳入的參數即是步驟1中創建的字段數組,如下: MatrixCursormatrixCursor=newMatrixCursor(COLUMN_NAME); 也可以指定初始大小,如: matrixCursor=newMatrixCursor( COLUMN_NAME, 10);
    步驟3. 通過matrixCursor 的addRow方法添加一行值,相當於向數據庫中插入一條記錄,如下: matrixCursor.addRow(new Object[] { R.drawable.ic_launcher, "zhangsan",39 });
    注:步驟3也可以通過構造一個MatrixCursor.RowBuilder來實現,也是相當於向數據庫中插入一條記錄,如下: MatrixCursor.RowBuilder builder1 = matrixCursor.newRow();
    builder1.add(R.drawable.ic_launcher);
    builder1.add("zhangsan");
    builder1.add(39);
    //builder1.add(399999); 通過上面三步即可完成MatrixCursor 的構造。從MatrixCursor 中取出數據的過程與Cursor相同,不再贅述!

    注釋:如果builder1對象add的數量大於字段的個數,即打開builder1.add(399999);注釋,那麼將會報以下錯誤:
    02-27 19:00:44.882: W/dalvikvm(29036): threadid=1: thread exiting with uncaught exception (group=0x41e689a8)
    02-27 19:00:44.882: W/dalvikvm(29036): threadid=1: uncaught exception occurred
    02-27 19:00:44.883: W/System.err(29036): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.test.matrixcursor/com.test.matrixcursor.MainActivity}:android.database.CursorIndexOutOfBoundsException: No more columns left.
    02-27 19:00:44.883: W/System.err(29036): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2356)
    02-27 19:00:44.883: W/System.err(29036): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2408)
    02-27 19:00:44.883: W/System.err(29036): at android.app.ActivityThread.access$600(ActivityThread.java:167)
    02-27 19:00:44.883: W/System.err(29036): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1378)
    02-27 19:00:44.883: W/System.err(29036): at android.os.Handler.dispatchMessage(Handler.java:107)
    02-27 19:00:44.883: W/System.err(29036): at android.os.Looper.loop(Looper.java:194)
    02-27 19:00:44.884: W/System.err(29036): at android.app.ActivityThread.main(ActivityThread.java:5405)
    02-27 19:00:44.884: W/System.err(29036): at java.lang.reflect.Method.invokeNative(Native Method)
    02-27 19:00:44.884: W/System.err(29036): at java.lang.reflect.Method.invoke(Method.java:525)
    02-27 19:00:44.884: W/System.err(29036): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:838)
    02-27 19:00:44.884: W/System.err(29036): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:605)
    02-27 19:00:44.884: W/System.err(29036): at dalvik.system.NativeStart.main(Native Method)
    02-27 19:00:44.886: W/System.err(29036): Caused by: android.database.CursorIndexOutOfBoundsException: No more columns left.
    02-27 19:00:44.887: W/System.err(29036): at android.database.MatrixCursor$RowBuilder.add(MatrixCursor.java:206)
    02-27 19:00:44.887: W/System.err(29036): at com.test.matrixcursor.MainActivity.createList(MainActivity.java:46)
    02-27 19:00:44.888: W/System.err(29036): at com.test.matrixcursor.MainActivity.onCreate(MainActivity.java:20)
    02-27 19:00:44.889: W/System.err(29036): at android.app.Activity.performCreate(Activity.java:5127)
    02-27 19:00:44.889: W/System.err(29036): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1151)
    02-27 19:00:44.890: W/System.err(29036): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2320) 程序運行結果:




  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved