Content Provider: Projection - Filtering - Sorting

Latihan ini masih tentang topik 'Content Provider' dan mengacu pada potongan kode MainActivity.java pada latihan sebelumnya.

Projection

Parameter kedua dari method 'managedQuery()' (parameter ketiga pada class 'CursorLoader') mengatur berapa banyak kolom yang akan dikembalikan/dihasilkan oleh query tersebut; parameter ini disebut dengan projection.
Pada kode sebelumnya di atas, kita menetapkan dengan null:
Cursor c;
if (Build.VERSION.SDK_INT < 11) {
/*sebelum honeycomb*/
c = managedQuery(allContacts, null, null, null, null);
} else {
/*Honeycomb dan seseudahnya*/
CursorLoader cursorLoader = new CursorLoader(this,allContacts,null,null,null,null);
c = cursorLoader.loadInBackground();
}
Tetapi kita juga bisa menetapkan kolom-kolom yang tepat untuk dikembalikan/dihasilkan dengan membuat array yang berisi nama kolom, seperti ini (perhatikan kode yang dicetak tebal):
String[] projection = new String[]
{ContactsContract.Contacts._ID,
ContactsContract.Contacts.DISPLAY_NAME,
ContactsContract.Contacts.HAS_PHONE_NUMBER};
 
Cursor c;
if (Build.VERSION.SDK_INT < 11) {
/*sebelum honeycomb*/
c = managedQuery(allContacts, projection, null, null, null);
} else {
/*Honeycomb dan seseudahnya*/
CursorLoader cursorLoader = new CursorLoader(this, allContacts, projection, null, null, null);
c = cursorLoader.loadInBackground();
}
Dalam hal ini (kode di atas), akan mengambil kolom-kolom _ID, DISPLAY_NAME, dan HAS_PHONE_NUMBER.


Filtering

Parameter ketiga dan keempat pada method 'managedQuery()' (parameter keempat dan kelima pada class 'CursorLoader') memungkinkan kita untuk membuat kondisi 'WHERE' dalam SQL untuk mem-filter hasil query. 
Contohnya, potongan kode berikut hanya akan menghasilkan nama contact yang berakhiran dengan "vi" (perhatikan kode yang dicetak tebal):
Cursor c;
if (Build.VERSION.SDK_INT < 11) {
/*sebelum honeycomb*/
c = managedQuery(allContacts, projection,
ContactsContract.Contacts.DISPLAY_NAME + " LIKE '%vi'", null, null);
} else {
/*Honeycomb dan seseudahnya*/
CursorLoader cursorLoader = new CursorLoader(this,allContacts,projection,
ContactsContract.Contacts.DISPLAY_NAME + " LIKE '%vi'", null, null);
c = cursorLoader.loadInBackground();
}
Kode tersebut, pada parameter ketiga di method 'managedQuery()', dan parameter keempat pada constructor 'CursorLoader' berisi kode SQL yang berisi nama berakhiran ("vi). Kita juga bisa membuat string pencarian ke dalam argument/parameter berikutnya dari method/constructor tersebut seperti berikut ini (perhatikan kode yang dicetak tebal):
Cursor c;
if (Build.VERSION.SDK_INT < 11) {
/*sebelum honeycomb*/
c = managedQuery(allContacts, projection,
ContactsContract.Contacts.DISPLAY_NAME + " LIKE ?", new String[]{"%vi"}, null);
} else {
/*Honeycomb dan seseudahnya*/
CursorLoader cursorLoader = new CursorLoader(this,allContacts,projection,
ContactsContract.Contacts.DISPLAY_NAME + " LIKE ?",new String[]{"%vi"},null);
c = cursorLoader.loadInBackground();
}

Sorting

Parameter terakhir dari method 'managedQuery()' dan constructor untuk class 'CursorLoader' memungkinkan kita untuk membuat kode SQL 'ORDER BY' untuk mengurutkan hasil query. Contohnya, kode berikut akan mengurutkan nama-nama contacts secara ascending (perhatikan kode yang dicetak tebal):
Cursor c;
if (Build.VERSION.SDK_INT < 11) {
/*sebelum honeycomb*/
c = managedQuery(allContacts, projection,
ContactsContract.Contacts.DISPLAY_NAME + " LIKE ?", new String[]{"%vi"}, ContactsContract.Contacts.DISPLAY_NAME + " ASC");
} else {
/*Honeycomb dan seseudahnya*/
CursorLoader cursorLoader = new CursorLoader(this,allContacts,projection,
ContactsContract.Contacts.DISPLAY_NAME + " LIKE ?",new String[]{"%vi"}, ContactsContract.Contacts.DISPLAY_NAME + " ASC");
c = cursorLoader.loadInBackground();
}

No comments: