Android 调用ContentObserver时更新设备联系人的页面列表
我正在使用ContentResolver和体系结构组件库LiveData、ViewModel和Paging检索具有分页功能的android设备联系人 我还注册了一个ContentObserver,以便在联系人发生更改时得到通知,以便能够更新我的联系人页面列表,但问题是我不知道怎么做Android 调用ContentObserver时更新设备联系人的页面列表,android,android-livedata,android-architecture-components,android-viewmodel,android-paging,Android,Android Livedata,Android Architecture Components,Android Viewmodel,Android Paging,我正在使用ContentResolver和体系结构组件库LiveData、ViewModel和Paging检索具有分页功能的android设备联系人 我还注册了一个ContentObserver,以便在联系人发生更改时得到通知,以便能够更新我的联系人页面列表,但问题是我不知道怎么做 class ContactsViewModel : ViewModel() { private val config by lazy { PagedList.Config.Builder()
class ContactsViewModel : ViewModel() {
private val config by lazy { PagedList.Config.Builder()
.setPageSize(20)
.setEnablePlaceholders(false)
.build()
}
val contacts by lazy {
LivePagedListBuilder<Int, Contact>(ContactsDataSourceFactory(), config).build()
}
}
class ContactsDataSourceFactory : DataSource.Factory<Int, Contact>() {
override fun create(): DataSource<Int, Contact> {
return ContactsDataSource()
}
}
class ContactsDataSource : PositionalDataSource<Contact>() {
companion object {
private val PROJECTION = arrayOf(
ContactsContract.Contacts._ID,
ContactsContract.Contacts.DISPLAY_NAME_PRIMARY,
ContactsContract.Contacts.PHOTO_URI,
ContactsContract.Contacts.LOOKUP_KEY
)
}
override fun loadInitial(params: LoadInitialParams, callback: LoadInitialCallback<Contact>) {
callback.onResult(getContacts(params.requestedLoadSize, params.requestedStartPosition), 0)
}
override fun loadRange(params: LoadRangeParams, callback: LoadRangeCallback<Contact>) {
callback.onResult(getContacts(params.loadSize, params.startPosition))
}
private fun getContacts(limit: Int, offset: Int): MutableList<Contact> {
val cursor = App.appContext.contentResolver.query(
ContactsContract.Contacts.CONTENT_URI,
PROJECTION,
null,
null,
"${ContactsContract.Contacts.DISPLAY_NAME_PRIMARY} ASC LIMIT $limit OFFSET $offset"
)
val contacts: MutableList<Contact> = mutableListOf()
cursor?.run {
while (moveToNext()) {
val id = getLong(getColumnIndex(PROJECTION[0]))
val name = getString(getColumnIndex(PROJECTION[1]))
val photoUri = getString(getColumnIndex(PROJECTION[2]))
val lookupKey = getString(getColumnIndex(PROJECTION[3]))
val lookupUri = ContactsContract.Contacts.getLookupUri(id, lookupKey)
contacts.add(Contact(id, name, photoUri, lookupUri))
}
close()
}
return contacts
}
}
我知道我不明白发生了什么变化
我只需要获取新列表并将其传递给DiffUtil,它将为我完成其余的工作。换句话说,我需要以某种方式刷新我当前的页面列表
如果有人给我一个正确的方法我会很感激的谢谢
private val contactsViewModel by lazy { ViewModelProviders.of(this).get(ContactsViewModel::class.java) }
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
activity?.contentResolver.registerContentObserver(
ContactsContract.Contacts.CONTENT_URI,
true,
OnContactsChangedListener(Handler())
)
contactsViewModel.contacts.observe(this, Observer { adapter.submitList(it) })
}
inner class OnContactsChangedListener(handler: Handler) : ContentObserver(handler) {
override fun onChange(selfChange: Boolean) {
//A change has occurred. What to do next?!!
}
}