【发布时间】:2023-01-05 20:50:58
【问题描述】:
所以...昨天我的代码工作得很好,但今天,我不知道发生了什么,它停止工作了。 游标为空,同时返回“”。我不知道该怎么办。
光标还能用吗?
@SuppressLint("Range", "Recycle")
@Composable
fun ContactPickerTwinTurbo(
done: (String, String) -> Unit
) {
val context = LocalContext.current
val launcher = rememberLauncherForActivityResult(
contract = ActivityResultContracts.PickContact(),
onResult = {
val contentResolver: ContentResolver = context.contentResolver
var name = ""
var number = ""
val cursor: Cursor? = contentResolver.query(it!!, null, null, null, null)
if (cursor != null) {
if (cursor.moveToFirst()) {
name =
cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME))
Log.d("Name", name)
val id = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID))
val phones: Cursor? = contentResolver.query(
Phone.CONTENT_URI, null,
Phone.CONTACT_ID + " = " + id, null, null
)
if (phones != null) {
while (phones.moveToNext()) {
number = phones.getString(phones.getColumnIndex(Phone.NUMBER))
Log.d("Number", number)
}
phones.close()
}
}
}
done(name, number)
}
)
Button(
onClick = {
launcher.launch()
},
modifier = Modifier
.fillMaxWidth()
.padding(10.dp)
) {
Text(text = "Pick Contact")
}
}
游标是如何工作的? 我需要等待吗? 我应该征得访问联系人的许可吗?
【问题讨论】:
标签: android kotlin android-jetpack-compose cursor jetpack