我想从Android联系人中获取ID,名称,生日,地址。我更改了uri(ContactsContract.CommonDataKinds,ContactsContract.Contacts等),并能够获取地址,但是生日消失了。无论如何,我都找不到一次同时获取两者的解决方案。
année: <select id="annee">
<option value="">--année--</option>
<option value="2019">2019</option>
<option value="2020">2020</option>
<option value="2021">2021</option>
<option value="2022">2022</option>
<option value="2023">2023</option>
<option value="2024">2024</option>
</select>
<?php var_dump($this->input->post('annee'));
var_dump($surcharges);
?>
以上代码的输出为:
public void getBirthdays() {
// get data from contacts
Uri uri = ContactsContract.Data.CONTENT_URI;
String[] projection = new String[]{
ContactsContract.CommonDataKinds.Event.CONTACT_ID,
ContactsContract.Contacts.DISPLAY_NAME,
ContactsContract.CommonDataKinds.Event.START_DATE,
ContactsContract.CommonDataKinds.StructuredPostal.FORMATTED_ADDRESS};
String selection =
ContactsContract.Data.MIMETYPE + "= ? AND " +
ContactsContract.CommonDataKinds.Event.TYPE + "=" + ContactsContract.CommonDataKinds.Event.TYPE_BIRTHDAY;
String[] selectionArgs = new String[]{ContactsContract.CommonDataKinds.Event.CONTENT_ITEM_TYPE};
Cursor cursor = getContentResolver().query(uri, projection, selection, selectionArgs, null);
int columnCount = cursor.getColumnCount();
cursor.moveToFirst();
for (int i = 0; i < columnCount; i++) {
Common.echo(cursor.getColumnName(i) + ", " + cursor.getString(i));
}
cursor.close();
}
“ data1”字段被列出两次,因为START_DATE(生日)和FORMATTED_ADDRESS都存储在同一虚拟列“ data1”中。
是否有一种方法可以在解析器中定义虚拟列名,例如与SQL等效的“ SELECT col1 AS name1,col2 AS name2,col3,col4 WHERE ...”?
答案 0 :(得分:0)
两个问题:您将查询限制为仅EVENT
s,并且只记录了查询中找到的第一行。
尝试一下:
private class Contact {
public Long contactId;
public String name;
public String birthday;
public String address;
}
public void getBirthdaysAndAddress() {
// mapping from contactId to a contact object
Map<Long, Contact> contacts = new HashMap<>();
Uri uri = Data.CONTENT_URI;
String[] projection = new String[]{
Data.CONTACT_ID,
Data.DISPLAY_NAME,
Data.MIMETYPE,
Data.DATA1,
Event.TYPE};
// get ALL rows that represent either a birthday or a postal address
String selection = Data.MIMETYPE + "=? OR " + Data.MIMETYPE + "=?";
String[] selectionArgs = new String[]{Event.CONTENT_ITEM_TYPE, StructuredPostal.CONTENT_ITEM_TYPE};
Cursor cursor = getContentResolver().query(uri, projection, selection, selectionArgs, null);
while (cursor.moveToNext()) {
Long id = cursor.getLong(0);
// get an existing contact object, or create a new one
Contact contact = contacts.get(id);
if (contact == null) {
contact = new Contact();
contact.contactId = id;
contact.name = cursor.getString(1);
contacts.put(id, contact);
}
String mimetype = cursor.getString(2);
if (mimetype.equals(Event.CONTENT_ITEM_TYPE)) {
// this is an event row, check that it is a birthday
int type = cursor.getInt(4);
if (type == Event.TYPE_BIRTHDAY) {
contact.birthday = cursor.getString(3);
}
} else {
// this is an address row
contact.address = cursor.getString(3);
}
}
cursor.close();
// print all contact objects containing a birthday
Iterator it = mp.values().iterator();
while (it.hasNext()) {
Contact contact = (Contact) it.next();
if (!TextUtils.isEmpty(contact.birthday)) {
Log.i("Birthday contacts", contact.contactId + " - " + contact.name + " - " + contact.birthday + " - " + contact.address);
}
}
}