티스토리 뷰

Content Provider는 Android의 특성상 Application간에 데이터 공유가 어려운데, 이를 쉽게 공유할 수 있도록 도와준다.


Content Provider 사용해야 하는 이유

  1. 앱의 코드 변경없이 데이터 저장소의 변경 가능
  2. Loader CursorAdapter 사용함.
  3. 다른 개발자들이 쉽게 접근할 있도록.


ContentResolver : Content Provider 엄청 많은데, 모든 데이터를 동기화하지 않으면 교통 체증이 생김. 컨텐트 프로바이더 간의 중개자 역할을 .


    ContentResolver contentResolver = getContentResolver();
    Cursor cursor = contentResolver.query(DroidTermsExampleContract.CONTENT_URI, null, null, null, null);

위와 같이 사용.


ContentResolver 4가지 메소드 (CRUD)

  1. query() : Read from data.
  2. insert() : Add a row or rows to the data.
  3. update() : Update the data.
  4. delete() : Delete a row or rows from the data.


URI : Uniform Resource Identifier

ex) content://com.example.udacity.droidtermsexamle/terms

  1. Content Provider Prefix : content:// == http://, https:// -> Content Provider 위한 Uri이다.
  2. Content Authority : com.~~.droidtermsexample -> 통신할 대상을 찾음
  3. Specific Data : /terms


Contract라고 약속해놓은 규약 형식이 있다.


Data는 Contract라는 규약을 통해 가져오면 Cursor의 형태로 반환되는데, 이는 Database의 Data와 가져오는 형식이 같다.


Cursor를 어떻게 사용하고, 적용하는지에 대해 보면


    mData = cursor;
    mData.moveToFirst();
    mData.moveToNext();
    String word = mData.getString(mData.getColumnIndex(DroidTermsExampleContract.COLUMN_WORD));

이런식의 'moveToFirst()', 'moveToNext()' 함수를 통해 row를 이동하고,

'getColumnIndex()'를 통해 column에 접근하여 값을 가져오면 된다.


이렇게 이용하면 됨!

'개발 > 안드로이드' 카테고리의 다른 글

Android Custom Content Provider  (0) 2017.11.01
Android SQLite Database  (3) 2017.10.27
Android Permissions  (0) 2017.10.25
Android Location Api  (0) 2017.10.24
Android Action bar Style Setting  (0) 2017.10.23
댓글
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31