gpt4 book ai didi

Android:使用ContentResolver,CONTACT_ID 是列还是行?

转载 作者:行者123 更新时间:2023-11-29 22:11:02 25 4
gpt4 key购买 nike

我的问题看起来很愚蠢,但下面的代码让我很头疼。下面的代码将 contactIdtelephone number 打印到屏幕。

它运作良好,但我需要更清楚地了解一些事情:

 ContentResolver solver = getContentResolver();
String mess="";

Cursor cursor = solver.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
while (cursor.moveToNext()){
String contactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
mess = mess + "ID: "+contactId+"\n";

Cursor phones = getContentResolver().query( ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = "+ contactId, null, null);
while (phones.moveToNext()) {
String phoneNumber = phones.getString(phones.getColumnIndex( ContactsContract.CommonDataKinds.Phone.NUMBER));
mess = mess + phoneNumber + "\n";
}
}

我不知道的是上面这行代码:

Cursor phones = getContentResolver().query( ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = "+ contactId, null, null);

Android开发中关于第三个参数:

selection : A filter declaring which rows to return, formatted as an SQL WHERE clause (excluding the WHERE itself). Passing null will return all rows for the given URI.

因此,作为此定义,CONTACT_ID 充当“A ROW”。 (因为它过滤要返回的行),

但是在这一行中,CONTACT_ID 充当“A COLUMN”:

String contactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));

请为我解释这一点。

谢谢:)

最佳答案

它是一个列(像这样声明 INTEGER PRIMARY KEY AUTOINCREMENT)。在:

Cursor phones = getContentResolver().query( 
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = "+ contactId,
null, null);

您查询 ContentProvider 但使用 _ID 列来过滤结果。您是说:“我想要 ContentProvider 中的行,在列 _ID 中我只找到值 contactId”。在:

String contactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));

_ID 用于获取列索引整数(从该列获取值),因此您不必使用像 0、1、2 这样的简单数字,并可能避免错误。

编辑:第二个参数(也称为投影)表示您要从提供者检索的数据列(null = 获取所有列)。您可以将第二个参数视为过滤器,您只会获得您在数组中指定的那些列(例如,您可能不想要多个列,因为您不会使用它们,因此对于第二个参数,您设置了一个字符串数组使用您确实想要的列并省略您不需要的列)。第三个参数过滤行,第二个参数过滤你检索的列()

关于Android:使用ContentResolver,CONTACT_ID 是列还是行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9648169/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com