0

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

以下是我的 Database 类中最有可能导致问题的部分:

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
4

0 回答 0