使用搜索查询时,Android FTS4数据库返回空光标

时间:2013-02-16 01:58:10

标签: android cursor fts4

在我的应用程序中,我创建了一个存储联系信息的FTS4数据库,该应用程序使用户能够根据名称搜索联系人。当用户输入搜索查询时,然后将该查询与数据库中的条目进行比较,然后将匹配的条目显示在列表上。当我点击任何一个条目时,它会给我一个错误,我调试它以确定我的光标是空的。

以下是我的数据库类的一部分,很可能导致问题:

private static final String DATABASE_CREATE_FTS = 
        "CREATE VIRTUAL TABLE " + DATABASE_TABLE_FTS + " USING fts4(" +
                 COL_ID + " TEXT, " +
                 COL_NAME + " TEXT, " + 
                 COL_EMAIL + " TEXT, " +
                 COL_CELL + " TEXT, " + 
                 COL_ARRIVAL + " TEXT, " +
                 COL_DEPARTURE + " TEXT, " + 
                 COL_FLIGHT_NUMBER + " TEXT, " +
                 COL_HOTEL_ROOM_NUMBER + " TEXT, " + 
                 COL_EVENT1 + " TEXT, " +
                 COL_EVENT2 + " TEXT, " + 
                 COL_EVENT1_ROOM + " TEXT, " +
                 COL_EVENT2_ROOM + " TEXT" + ");";


public Cursor getContactMatches(String query, String[] columns) {
            String selection = COL_NAME + " MATCH ?";
            Log.d("selection", selection);
            String[] selectionArgs = new String[] {query};

            return query(selection, selectionArgs, columns);
        }

        public Cursor query(String selection, String[] selectionArgs, String[] columns) {
            DBHelper = new DbHelper(ourContext);
            open();
            SQLiteQueryBuilder build = new SQLiteQueryBuilder();
            build.setTables(DATABASE_TABLE_FTS);
            SQLiteDatabase data = DBHelper.getWritableDatabase();
            Cursor cursor = build.query(data,
                    columns, selection, selectionArgs, null, null, null);
            Log.d("cursor" , "" + cursor.getCount());
            return cursor;
        }

这是我的搜索活动:

private void showResults(String query) {
    db = new DatabaseTable(this);
    db.open();
    Cursor cursor = db.getContactMatches(query, DatabaseTable.COLUMNS);
    cursor.moveToFirst();
    int name = cursor.getColumnIndex(DatabaseTable.COL_NAME);
    Log.d("Name", "" + name);
    mAdapter = new SimpleCursorAdapter(this, R.layout.activity_search_results, 
            cursor, new String[] {DatabaseTable.COL_NAME}, new int[] {android.R.id.text1}, 0);
    list.setAdapter(mAdapter);
    Log.d("Cursor", "" + cursor.getCount());
    getLoaderManager().initLoader(0, null, this);
    list.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Intent contactIntent = new Intent(view.getContext(), ContactActivity.class);
            Uri uri = Uri.withAppendedPath(SomeProvider.CONTENT_URI, String.valueOf(id));
            contactIntent.setData(uri);
            startActivity(contactIntent);
        }
    });
    db.close();
}

错误:

02-15 19:56:22.176: D/cursor(31943): android.database.sqlite.SQLiteCursor@41cdde90
02-15 19:56:22.176: D/c(31943): 0 (amounts of rows)
02-15 19:56:22.176: D/Index(31943): 1
02-15 19:56:22.176: D/AndroidRuntime(31943): Shutting down VM
02-15 19:56:22.176: W/dalvikvm(31943): threadid=1: thread exiting with uncaught exception (group=0x40ea5438)
02-15 19:56:22.186: E/AndroidRuntime(31943): FATAL EXCEPTION: main
02-15 19:56:22.186: E/AndroidRuntime(31943): java.lang.RuntimeException: Unable to    start activity ComponentInfo

{com.example.myfirstapp/com.example.myfirstapp.ContactActivity}: android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0

0 个答案:

没有答案