Cursor 객체 레코드의 집합.
android.database.Cursor : This interface provides random read-write access to the result set returned by a database query. Cursor implementations are not required to be synchronized so code using a Cursor from multiple threads should perform its own synchronization when using the Cursor.
ContentProvider 안드로이드 어플리케이션에서 어플리케이션간에 데이터를 공유할수 있는 기본적인 방법
android.content.ContentProvider : Content providers are one of the primary building blocks of Android applications, providing content to applications. They encapsulate data and provide it to applications through the single ContentResolver interface. A content provider is only required if you need to share data between multiple applications. For example, the contacts data is used by multiple applications and must be stored in a content provider. If you don't need to share data amongst multiple applications you can use a database directly via SQLiteDatabase.
When a request is made via a ContentResolver the system inspects the authority of the given URI and passes the request to the content provider registered with the authority. The content provider can interpret the rest of the URI however it wants. The UriMatcher class is helpful for parsing URIs.
The primary methods that need to be implemented are:
onCreate()
which is called to initialize the providerquery(Uri, String[], String, String[], String)
which returns data to the callerinsert(Uri, ContentValues)
which inserts new data into the content providerupdate(Uri, ContentValues, String, String[])
which updates existing data in the content providerdelete(Uri, String, String[])
which deletes data from the content providergetType(Uri)
which returns the MIME type of data in the content provider
Data access methods (such as insert(Uri, ContentValues)
and update(Uri, ContentValues, String, String[])
) may be called from many threads at once, and must be thread-safe. Other methods (such asonCreate()
) are only called from the application main thread, and must avoid performing lengthy operations. See the method descriptions for their expected thread behavior.
Requests to ContentResolver
are automatically forwarded to the appropriate ContentProvider instance, so subclasses don't have to worry about the details of cross-process calls.
ContentResolver : 이 클래스는 content model 에 어플리케이션들이 접근할 수 있게 제공한다. (Content Provider를 이용 하여 실제적으로 데이터를 삽입,삭제,수정하는 객체)
android.content.ContentResolver : This class provides applications access to the content model.
2) ContentResolver개체를 생성하고 Uri를 조회하여 Cursor 객체를 반환한다.
3) Cursor 객체를 조회하여 데이터를 확인한다.
Source ContentResolver 객체를 생성하여 Uri를 조회 Cursor 객체를 반환한다.
private Cursor getMusicDataList(){
ContentResolver cr = getContentResolver();
Uri audioExternalUri = android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
Uri audioInternalUri = android.provider.MediaStore.Audio.Media.INTERNAL_CONTENT_URI; Uri audioAllUri = Uri.parse(android.provider.MediaStore.Audio.Media.CONTENT_TYPE);
// "vnd.android.cursor.dir/audio"
String sortOrder = android.provider.MediaStore.Audio.Media.TITLE +" ASC";
Cursor cursor = cr.query(audioAllUri , null, null, null, sortOrder);
if(null==cursor){
LOG.d(CLASSNAME,"cursor is null");
} else {
LOG.d(CLASSNAME,"cursor.getPosition() : "+ cursor.getPosition()); //A
printColumnNameForCursor("",cursor);
}
LOG.d(CLASSNAME,"cursor.getPosition() 1 : "+ cursor.getPosition()); //B
cursor.moveToFirst();
LOG.d(CLASSNAME,"cursor.getPosition() 2 : "+ cursor.getPosition()); //C
printCursorData("",cursor);
return cursor;
}
최초 반환된 Cursor 객체는 null 이 아니라면 -1의 포지션을 가진다.
*A,B 값 확인 시 -1
Cursor.moveToFirst() 함수 호출시 첫번째 poistion 즉 0으로 이동 된다.
*C 값 확인 시 0
**데이터를 확인하기 위해 반복문 돌리게 될 경우 위 내용을 주의 해야한다.
Source 컬럼 값을 확인 하기 위해 작성한 Helper 함수 printColumnNameForCursor
public static void printColumnNameForCursor(String tableName,Cursor cursor){
LOG.d(CLASSNAME,"printColumnNameForCursor");
LOG.d(CLASSNAME,"==============START tableName : +tableName+"============================");
LOG.d(CLASSNAME,"Cursor.getCount() :"+cursor.getCount() +")"); //전체 레코드 갯수
for (int i = 0; i < cursor.getColumnCount(); i++) {
LOG.d(CLASSNAME,"["+ i +"] ,COLUMN:" + cursor.getColumnName(i));
}
LOG.d(CLASSNAME,"==============END tableName : "+tableName+"==============================");
}
Source 데이터 값을 확인 하기 위해 작성한 Helper 함수 printCursorData
public static void printCursorData(String tableName, Cursor cursor) {
LOG.i(CLASSNAME,"printCursorData");
LOG.d(CLASSNAME,"============START tableName : "+tableName+"==============================");
int rowNum=0;
for( boolean haveRow = cursor.moveToFirst(); haveRow; haveRow = cursor.moveToNext() ) {
//row(행) 갯수 만큼 돌아 모든 데이터를 출력하도록 함
rowNum++;
StringBuilder sb = new StringBuilder();
for (int i = 0; i < cursor.getColumnCount(); i++) {
//column(열) 갯수 만큼 돌아 한 행의 데이터를 한줄 문자열로 만듬.
sb.append(String.format("%-15s", cursor.getString(cursor.getColumnIndex(cursor.getColumnName(i)))));
}
LOG.d(CLASSNAME,"rowNum ["+rowNum+"] :" + sb.toString());
sb = null;
}
LOG.d(CLASSNAME,"==============END tableName : "+tableName+"==============================");
cursor.moveToFirst(); // cursor를 첫째 위치로 이동 0 ==>CusorAdapter 정상동작 확인 . OK
}
'PROGRAMING > Android' 카테고리의 다른 글
android.content.CursorLoader 와 LoaderCallbacks 인터페이스 (0) | 2013.02.22 |
---|---|
[widget]android.widget.CursorAdapter, ViewHolder pattern (0) | 2013.02.19 |
[widget]android.widget.ListView [3] convertView 사용과 Holder Pattern (0) | 2013.02.18 |
[widget]android.widget.ListView [2] CustomArrayAdapter --getView (0) | 2013.02.18 |
[widget]android.widget.ListView [1] (0) | 2013.02.18 |