2017-05-19 3 views
-1

채울 가져올 수 없습니다 :는 SimpleCursorAdapter가있는 ListView에게 내가 단순한 컨텐츠 제공 업체를 작성하고 이러한 참조를 사용하여 목록보기 채우기 위해 노력하고

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

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

나는이 스레드에서보고를하지만 않습니다

SimpleCursorAdapter to populate ListView

날엔 내 문제가 될 것 같지 abase가 작동하는 것처럼 보일 수 있지만 ListView에 바인딩하려고하면 누락 된 열 '_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);'메소드에서 커서를 닫는 것을 의미합니다. –

답변

관련 문제