2017-05-19 14 views
-1

を移入するために得ることができない私は、単純なコンテンツプロバイダを作成し、これらの参照を使用してリストビューを移入しようとしています:がSimpleCursorAdapterは、リストビュー

https://developer.android.com/reference/android/app/ListActivity.html

http://www.newthinktank.com/2015/01/make-android-apps-21/

私はこのスレッドを見たが、それはありません

SimpleCursorAdapter to populate ListView

DAT:私の問題ではないように見えます私のリストビューにバインドしようとすると、列 '_id'が見つからないというエラーが出ますが、データベースの内容を問題なくログに記録することができます。以下のコードスニペット:データベースのログ

(この作品!):

public void logAllPatients() { 
    // Projection contains the columns we want 
    String[] projection = new String[]{"id", "name"}; 
    // Pass the URL, projection and I'll cover the other options below 
    Cursor cursor = resolver.query(CONTENT_URL, projection, null, null, null); 
    // Cycle through and display every row of data 
    if (cursor.moveToFirst()) { 
     do { 
      String patientList = ""; 
      String id = cursor.getString(cursor.getColumnIndex("id")); 
      String name = cursor.getString(cursor.getColumnIndex("name")); 
      patientList = patientList + id + " : " + name + "\n"; 
      Log.d(TEST_CONTENT_PROVIDER, patientList); 
     } while (cursor.moveToNext()); 
    } 
} 

は、リストビューを移入しようとしている(なぜ?、列が不足して)?

private void bindAllPatients() { 
    try { 
     // Projection contains the columns we want 
     String[] projection = new String[]{"id", "name"}; 
     Cursor cursor = resolver.query(CONTENT_URL, projection, null, null, null); 
     if (cursor != null) { 
      startManagingCursor(cursor); 
      cursor.moveToFirst(); 
      // Now create a new list adapter bound to the cursor. 
      // SimpleListAdapter is designed for binding to a Cursor. 
      ListAdapter adapter = new SimpleCursorAdapter(
        this, // Context. 
        android.R.layout.two_line_list_item, 
        cursor,            // Pass in the cursor to bind to. 
        new String[]{"id", "name"},   // Array of cursor columns to bind to. 
        new int[]{R.id.my_id, R.id.my_name}, 0); 

      // Parallel array of which template objects to bind to those columns. 
      // Bind to our new adapter. 
      setListAdapter(adapter); 
      cursor.close(); 
     } 
    } catch (Exception e) { 
     Log.e(TEST_CONTENT_PROVIDER, e.toString()); 
    } 
} 

出力ログ:

private SQLiteDatabase sqlDB; 
static final String DATABASE_NAME = "myPatients"; 
static final String TABLE_NAME = "patients"; 
static final String CREATE_DB_TABLE = "CREATE TABLE " + TABLE_NAME + 
     "(id INTEGER PRIMARY KEY AUTOINCREMENT, " + " name TEXT NOT NULL);"; 

    // bunch of code 

    @Override 
    public void onCreate(SQLiteDatabase sqlDB) { 
     try { 
      sqlDB.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME); 
      sqlDB.execSQL(CREATE_DB_TABLE); 
     } catch (Exception e) { 
      Log.e(TEST_CONTENT_PROVIDER, e.toString()); 
     } 
    } 

と働くクエリオーバーライド、:

D/GMO_CONTENT_PROVIDER: 9 : Joe 
D/GMO_CONTENT_PROVIDER: 10 : Mary 
E/GMO_CONTENT_PROVIDER: java.lang.IllegalArgumentException: column '_id' does not exist 

ここでは、データベースの作成です!

@Nullable 
@Override 
public Cursor query(@NonNull Uri uri, @Nullable String[] projection, @Nullable String selection, @Nullable String[] selectionArgs, @Nullable String sortOrder) { 
    SQLiteQueryBuilder queryBuilder = new SQLiteQueryBuilder(); 
    queryBuilder.setTables(TABLE_NAME); 
    switch (uriMatcher.match(uri)) { 
     case uriCode: 
      queryBuilder.setProjectionMap(values); 
      break; 
     default: 
      throw new IllegalArgumentException("Unknown URI " + uri); 
    } 
    Cursor cursor = queryBuilder.query(sqlDB, projection, selection, selectionArgs, null, null, sortOrder); 
    cursor.setNotificationUri(getContext().getContentResolver(), uri); 
    return cursor; 
} 

ご協力いただきありがとうございます。

+0

「startManagingCursor(カーソル);」とは何ですか?使用後は必ずカーソルを閉じるようにしてください。 –

+0

@MuraliPrajapatiもちろん、カーソルを 'CursorAdapter'で使用するときは' close() 'できません。 – pskink

+0

@pskink' startManagingCursor(cursor); 'メソッドでカーソルを閉じることを意味します。 –

答えて

0

正解:

Android column '_id' does not exist?

は_id "SimpleCursorAdapterは、カーソルの結果セットが正確という名前の列を含まなければならないことを要求 ""。"

関連する問題