2013-02-22 1 views
0
I합니다 (_id 필드가) 테이블이있는 데이터베이스에서 커서를 검색하려고

하지만 simplecursoradapter을 _ID하지 생성 할 수없는 오류가가 그러한 열이 있기 때문에 SimpleCursorAdapter에 DB에서 커서를로드 할 수 없습니다 :

을 말한다
SqlliteException: no such column: _id 

하지만 내 데이터베이스 클래스의 잘못 : 커서를 반환

//The columns we'll include in the dictionary table 
public static final String COLUMN_ID = "_id"; 
public static final String COL_WORD = "WORD"; 
public static final String COL_DEFINITION = "DEFINITION"; 


private static final String FTS_TABLE_CREATE = 
       "CREATE VIRTUAL TABLE " + FTS_VIRTUAL_TABLE + 
       " USING fts3 (" + COLUMN_ID + " integer primary key autoincrement, " + 
       COL_WORD + ", " + 
       COL_DEFINITION + ")"; 

그리고

public Cursor getWordMatches(String query, String[] columns) { 
    String selection = COL_WORD + " MATCH ?"; 
    String[] selectionArgs = new String[] {query+"*"}; 

    return query(selection, selectionArgs, columns); 
} 

private Cursor query(String selection, String[] selectionArgs, String[] columns) { 
    SQLiteQueryBuilder builder = new SQLiteQueryBuilder(); 
    builder.setTables(FTS_VIRTUAL_TABLE); 

    String[] col = new String[] {COLUMN_ID, COL_WORD}; 
    //ubacio null - col 
    Cursor cursor = builder.query(mDatabaseOpenHelper.getReadableDatabase(), 
      col, selection, selectionArgs, null, null, null); 

    if (cursor == null) { 
     return null; 
    } else if (!cursor.moveToFirst()) { 
     cursor.close(); 
     return null; 
    } 
    return cursor; 
} 

이리스트 뷰를 보여줍니다 내 활동

  //query the db class method getWordMatches() 
     Cursor c = db.getWordMatches(query, null); 
      displayWords(c); 

     } 
    } 


public void displayWords(Cursor c){ 

    // Creates a new SimpleCursorAdapter 
    SimpleCursorAdapter mCursorAdapter = new SimpleCursorAdapter(
     getApplicationContext(),     // The application's Context object 
     android.R.layout.simple_list_item_1,  // A layout in XML for one row in the ListView 
     c,           // The result from the query 
     new String[] {DatabaseTable.COL_WORD},  // A string array of column names in the cursor 
     new int[] { android.R.id.text1 });   // An integer array of view IDs in the row layout 


    // Sets the adapter for the ListView 
    setListAdapter(mCursorAdapter); 

} 

당신은 안드로이드의 _id에 FTS 'rowid을 변경 별칭을 사용할 필요가

답변

3

http://developer.android.com/training/search/search.html에서 튜토리얼은 다음과 같습니다

String[] col = new String[] {"rowid as " + COLUMN_ID, COL_WORD}; 

FTS는 아무튼 "integer primary key autoincrement"은 전체적으로 "syntactic sugar"으로 무시되므로 기본 키의 기본 열 이름을 재정의 할 수 없습니다. 의 요구 사항으로

private static final String FTS_TABLE_CREATE = 
      "CREATE VIRTUAL TABLE " + FTS_VIRTUAL_TABLE + 
      " USING fts3 (" + COL_WORD + ", " + COL_DEFINITION + ")"; 
+0

감사 샘, 그것은 작동합니다 :) – CicPot

0

: 사실, FTS는 단순히

그래서 당신이 그렇지 않으면 추가하여 만들 테이블 문에서 추가 _id 열을 _id을 삭제해야 ... 일반 열로 _id 열 해석 CursorAdpater 클래스에서 기본 Cursor 객체에는 "_id"라는 열이 포함되어야합니다. 그 이유는이 오류가 발생하기 때문입니다. 이것은 바로 CursorAdapter Doc : http://developer.android.com/reference/android/widget/CursorAdapter.html