Pro Android with Kotlin


Download 5.42 Mb.
Pdf ko'rish
bet137/223
Sana04.11.2023
Hajmi5.42 Mb.
#1746807
1   ...   133   134   135   136   137   138   139   140   ...   223
Bog'liq
@de android telegram Pro Android with Kotlin Developing Modern Mobile

156
CHAPTER 8: APIs
Reading Contacts
For reading a number of contacts based on some criterion, you should create a loader 
as described in the “Loaders” section. To improve the code quality a little bit, we put the 
loading responsibility on our own class and write the following:
import android.app.Activity
import android.app.LoaderManager
import android.content.CursorLoader
import android.content.Loader
import android.database.Cursor
import android.os.Bundle
import android.provider.ContactsContract
import android.util.Log
import android.net.Uri.withAppendedPath
class ContactsLoader(val actv: Activity?,
val search:String):
LoaderManager.LoaderCallbacks {
override fun onCreateLoader(id: Int, args: Bundle?):
Loader? {
Log.e("LOG", "onCreateLoader()")
val PROJECTION = arrayOf(
ContactsContract.Contacts._ID,
ContactsContract.Contacts.LOOKUP_KEY,
ContactsContract.Contacts.DISPLAY_NAME_PRIMARY)
val SELECTION =
ContactsContract.Contacts.DISPLAY_NAME_PRIMARY
+ " LIKE ?"
val selectionArgs = arrayOf("%" + search + "%")
val contentUri =
ContactsContract.Contacts.CONTENT_URI
Log.e("LOG", contentUri.toString())
// Starts the query
return CursorLoader(
actv,
contentUri,
PROJECTION,
SELECTION,
selectionArgs,
null
)
}
override fun onLoadFinished(loader: Loader,
data: Cursor) {
Log.e("LOG", "Load finished: " + data)
if(data.moveToFirst()) {


157
CHAPTER 8: APIs
do {
Log.e("LOG", "Entry:")
data.columnNames.forEachIndexed { i, s ->
Log.e("LOG", " -> " + s + " -> "
+ data.getString(i))
}
} while (data.moveToNext())
}
// show on UI or other actions...
}
override fun onLoaderReset(loader: Loader?) {
Log.e("LOG", "onLoaderReset()")
// remove from UI or other actions...
}
}
By virtue of ContactsContract.Contacts.CONTENT_URI that we use here as a URI, this will do 
a search in the Contacts table, returning basic contacts data.
To initialize and start the loader, all that is left to do, for example, in your activity is this:
val searchStr = "" // or whatever
val ldr = ContactsLoader(this, searchStr)
loaderManager.initLoader(0, null, ldr)
If instead you want to do a search inside the Data table, which contains phone numbers, 
e-mail addresses, and more, you write the following in ContactsLoader.onCreateLoader():
...
val PROJECTION = arrayOf(
ContactsContract.Data._ID,
ContactsContract.Data.DISPLAY_NAME_PRIMARY,
ContactsContract.CommonDataKinds.Email.ADDRESS)
val SELECTION =
ContactsContract.CommonDataKinds.Email.ADDRESS
+ " LIKE ? " + "AND "
+ ContactsContract.Data.MIMETYPE + " = '"
+ ContactsContract.
CommonDataKinds.Email.CONTENT_ITEM_TYPE
+ "'"
val selectionArgs = arrayOf("%" + search + "%")
val contentUri = ContactsContract.Data.CONTENT_URI
Log.e("LOG", contentUri.toString())
...
There are also special URIs you can use. For example, for finding contacts by e-mail 
address, you could use the content URI ContactsContract.CommonDataKinds.Email.
CONTENT_URI.



Download 5.42 Mb.

Do'stlaringiz bilan baham:
1   ...   133   134   135   136   137   138   139   140   ...   223




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling