英文:
Pick Contacts starting from a specific alphabet from phone contact list in android studio
问题
根据标题所述,我想从手机联系人中选择以给定字母开头的联系人列表。
例如:如果我只想选择以字母"A"开头的联系人,我尝试的当前代码如下:
val selecteContactsList: ArrayList<SelectedContactItem> = arrayListOf()
val cursor = context.contentResolver.query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
arrayOf(
ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
ContactsContract.CommonDataKinds.Phone.NUMBER
),
ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + " LIKE ?",
arrayOf("A%"),
ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME
)
while (cursor!!.moveToNext()) {
val contactName =
cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME))
val contactNumber =
cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER))
val contactItem = SelectedContactItem(contactNumber, contactName)
selecteContactsList.add(contactItem)
}
cursor.close()
但是这不会产生任何输出。总选择的联系人计数为0。
如有帮助,将不胜感激。提前谢谢。
英文:
As the title says, I want to select contacts list from the phone contacts which start from the given alphabet.
for example: if i want to select only the contacts starting with A, my current code which i tried is:
val selecteContactsList: ArrayList<SelectedContactItem> = arrayListOf()
val cursor = context.contentResolver.query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
arrayOf(
ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
ContactsContract.CommonDataKinds.Phone.NUMBER
),
ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + " = ?",
arrayOf("A"),
ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME
)
while (cursor!!.moveToNext()) {
val contactName =
cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME))
val contactNumber =
cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER))
val contactItem = SelectedContactItem(contactNumber, contactName)
selecteContactsList.add(contactItem)
}
cursor.close()
But this does not give any output. total selected contacts count is 0.
Any help would be appreciated. Thanks in advance.
答案1
得分: 1
Phone.DISPLAY_NAME + " = ?",
arrayOf("A"),
意味着 '返回所有姓名为A的联系人'。我认为你想要询问 '返回所有姓名以字母A开头的联系人',这是一个不同的查询。
要实现这个目标,你可以将代码更改为:
Phone.DISPLAY_NAME + " LIKE 'A%'"
如果你想要不区分大小写的查询(即包括以小写字母'a'开头的姓名),可以尝试:
Phone.DISPLAY_NAME + " LIKE 'A%' COLLATE NOCASE"
但是,如果你的目标是显示所有联系人的列表,并且有一个滚动侧边栏,用户可以触摸字母"C"并跳转到以该字母开头的联系人姓名,我不建议使用这样的查询,而是应该查询设备上的所有联系人,并将它们全部显示在一个可滚动的大列表中,然后当用户触摸字母按钮时,滚动到列表中的某个位置。
这样的用户体验会更出色。
<details>
<summary>英文:</summary>
the part:
Phone.DISPLAY_NAME + " = ?",
arrayOf("A"),
means 'return all contacts with name A'.
I think you want to ask 'return all contacts with name starting with the letter A' which is a different query.
To do that you can change the code to:
Phone.DISPLAY_NAME + " LIKE 'A%'"
If you want a case insensitive query (i.e. include names that begin with small 'a'), try:
Phone.DISPLAY_NAME + " LIKE 'A%' COLLATE NOCASE"
However, if your goal is to show a list of all contacts, and have a scroller sidebar where the user can touch the letter "C" and jump to contact names beginning with that letter I would not suggest to have such queries, instead query for **all** contacts on the device, and show them all in one big scrollable list, and have those letters buttons scroll to a certain position in the list when touched.
The user experience would be far superior.
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论