1,在ActionBar里面加入搜索框
在ActionBar里面加入搜索框是现在APP应用的常用做法。要想把搜索做好,可不是那么容易。涉及到自动提示和真正的搜索。下图是我对一个相对完整的搜索框涉及的元素关系做了一个图,箭头只表示它们之间有关系,不是类图中的继承。
一个Activity会有一个ActionBar,默认情况下。你可以在ActionBar里面添加菜单条目。菜单条目可以设置标题,图标以及关联的ActionView,这里我们关联了一个SearchView。SearchView和SearchableInfo关联,和搜索服务关联。同时为了更好的处理关于SearchView的事件响应,我们抽象了一个控制类MovieSearch。它是逻辑控制中心,有搜索完了的跳转SearchActivity(包含SearchFregment),也有内容提示。内容提示主要是根据输入的字进行联想,或者根据历史记录或者热门搜索。所以这里有个SearchHistory,它又和数据表相关。
在加载自动提示的地方,我们用到了一个类MatrixCursor,所以,我们详细的了解这个类的用法。
2,MatrixCursor详解
假如有一个如下的数据库表结构
_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这个东西
见示例代码如下:
- <span style="font-size:14px;">package com.test.matrixcursor;
- import android.app.ListActivity;
- import android.database.MatrixCursor;
- import android.os.Bundle;
- import android.support.v4.widget.SimpleCursorAdapter;
- import android.view.View;
- import android.widget.ListView;
- import android.widget.Toast;
- public class MainActivity extends ListActivity {
- private static final String[] COLUMN_NAME = { "_id", "name", "price" };
- private MatrixCursor matrixCursor;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- createList();
- //createList2();
- }
- private void createList() {
- matrixCursor = new MatrixCursor(COLUMN_NAME, 1);
- startManagingCursor(matrixCursor);
- matrixCursor.addRow(new Object[] { R.drawable.ic_launcher, "zhangsan",
- 39 });
- matrixCursor
- .addRow(new Object[] { R.drawable.ic_launcher, "lisi", 40 });
- matrixCursor
- .addRow(new Object[] { R.drawable.ic_launcher, "wangwu", 41 });
- matrixCursor.addRow(new Object[] { R.drawable.ic_launcher, "zhaoliu",
- 42 });
- matrixCursor
- .addRow(new Object[] { R.drawable.ic_launcher, "sunqi", 43 });
- setListAdapter(new SimpleCursorAdapter(this, R.layout.activity_main,
- matrixCursor, COLUMN_NAME, new int[] { R.id.icon, R.id.name,
- R.id.price }));
- }
- private void createList2() {
- matrixCursor = new MatrixCursor(COLUMN_NAME, 1);
- startManagingCursor(matrixCursor);
- MatrixCursor.RowBuilder builder1 = matrixCursor.newRow();
- builder1.add(R.drawable.ic_launcher);
- builder1.add("zhangsan");
- builder1.add(39);
- MatrixCursor.RowBuilder builder2 = matrixCursor.newRow();
- builder2.add(R.drawable.ic_launcher);
- builder2.add("lisi");
- builder2.add(40);
- MatrixCursor.RowBuilder builder3 = matrixCursor.newRow();
- builder3.add(R.drawable.ic_launcher);
- builder3.add("wangwu");
- builder3.add(41);
- setListAdapter(new SimpleCursorAdapter(this, R.layout.activity_main,
- matrixCursor, COLUMN_NAME, new int[] { R.id.icon, R.id.name,
- R.id.price }));
- }
- @Override
- protected void onListItemClick(ListView l, View v, int position, long id) {
- super.onListItemClick(l, v, position, id);
- matrixCursor.moveToPosition(position);
- StringBuilder builder = new StringBuilder();
- builder.append("Name:")
- .append(matrixCursor.getString(matrixCursor
- .getColumnIndex("name"))).append("\n");
- builder.append("Price:")
- .append(matrixCursor.getString(matrixCursor
- .getColumnIndex("price"))).append("\n");
- Toast.makeText(getApplicationContext(), builder.toString(), 1000)
- .show();
- }
- }
- </span>
实现步骤仅需3步,下面通过字符数组来说明:
步骤1.首先创建一个字符数组,且字符数组的值对应着表的字段,如下:
String[] COLUMN_NAME = { "_id", "name", "price" };
步骤2.利用MatrixCursor的构造方法,构造一个MatrixCursor,传入的参数即是步骤1中创建的字段数组,如下:
MatrixCursor
matrixCursor=
new
MatrixCursor(
COLUMN_NAME
);
也可以指定初始大小,如:
matrixCursor
=
new
MatrixCursor(
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(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)
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)
程序运行结果: