Pro Android with Kotlin


CHAPTER 6: Content Providers


Download 5.42 Mb.
Pdf ko'rish
bet69/223
Sana04.11.2023
Hajmi5.42 Mb.
#1746807
1   ...   65   66   67   68   69   70   71   72   ...   223
Bog'liq
@de android telegram Pro Android with Kotlin Developing Modern Mobile

72
CHAPTER 6: Content Providers
// The content URI for items
@JvmField
val CONTENT_URI = Uri.withAppendedPath(
MyContentContract.CONTENT_URI, "items")
// The MIME type of a directory of items
@JvmField
val CONTENT_TYPE =
ContentResolver.CURSOR_DIR_BASE_TYPE +
"/vnd." + MyContentContract.AUTHORITY +
".items"
// The mime type of a single item.
@JvmField
val CONTENT_ITEM_TYPE =
ContentResolver.CURSOR_ITEM_BASE_TYPE +
"/vnd." + MyContentContract.AUTHORITY +
".items"
// You could add database column names or
// projection specifications here, or sort
// order specifications, and more
// ...
}
}
}
Of course, part of your interface design must be using meaningful names for the classes
field names, and field values.
Here are a couple of notes about this construct:

If you can make sure clients will be using only Kotlin as a platform, this 
can be written in a much shorter way without any boilerplate code.
object MyContentContract2 {
val AUTHORITY = "com.xyz.whatitis"
val CONTENT_URI = Uri.parse("content://"
+ AUTHORITY)
val SELECTION_ID_BASED =
BaseColumns._ID + " = ? "
object Items {
val NAME = "item_name"
val CONTENT_URI =
Uri.withAppendedPath(
MyContentContract.CONTENT_URI, "items")
Note The interface described in the contract class does not have to correspond to actual 
database tables. It is completely feasible and beneficial to conceptually decouple the interface from 
the actual implementation and also provide table joins or other item types here derived in any way 
you might think of.


73
CHAPTER 6: Content Providers
val CONTENT_TYPE =
ContentResolver.CURSOR_DIR_BASE_TYPE +
"/vnd." + MyContentContract.AUTHORITY +
".items"
val CONTENT_ITEM_TYPE =
ContentResolver.CURSOR_ITEM_BASE_TYPE +
"/vnd." + MyContentContract.AUTHORITY +
".items"
}
}
However, if we want Java clients to use the interface as well, we have to 
use all those companion object and @JvmObject declarations and modifiers.

Using companion objects and JvmObject annotations allows for writing 
TheClass.THE_FIELD like for static fields in Java.

You might consider providing an equivalent Java construct to your 
clients so they don’t have to learn the Kotlin syntax if they use only Java.

The Uri.parse() and Uri.withAppendedPath() method calls are just 
two examples of using the Uri class. The class contains a couple more 
methods that help to manage constructing correct URIs.

You can also provide helper methods inside the contract class. If you 
do so, make sure the interface class does not depend on other classes 
and add a modifier JvmStatic to the fun function declaration to make it 
callable from Java.
You would then provide this contract class (or classes, if you want to document the interface 
using both Kotlin and Java) publicly to any possible clients that are supposed to use your 
content provider app.
A Cursor Class Based on AbstractCursor and Related Classes
All the query*() methods from ContentProvider return an android.database.Cursor object. From 
the package you can see that this is a database-centric class, which is actually a small design 
flaw of Android since the content interface should have been access methodology agnostic.
In addition, the Cursor interface is a random access interface for clients wanting to scan 
through result sets. You can use the base implementation android.database.AbstractCursor 
for your cursor class; it already implements a couple of the interface methods. To do 
so, write class MyCursor : AbstractCursor { ... } or val myCursor = object : 
AbstractCursor { ... } and implement all abstract methods and overwrite some of the 
other methods for the class to do meaningful things.

override fun getCount(): Int
This specifies the number of data sets available.

override fun getColumnNames(): Array
This specifies the ordered array of column names.



Download 5.42 Mb.

Do'stlaringiz bilan baham:
1   ...   65   66   67   68   69   70   71   72   ...   223




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