gpt4 book ai didi

android - 从联系人 ID : android 中检索电话号码

转载 作者:搜寻专家 更新时间:2023-11-01 09:10:07 24 4
gpt4 key购买 nike

一旦从我的 ListView 中单击联系人姓名,我就会尝试获取联系人数据。按照下面的代码,我可以成功记录我的联系人 ID,但还没有设法使用 ContactsContract 来检索数据。最好的方法是什么? (试过 Retrieve Contact Phone Number From URI in Android 没有多大用处)

编辑 2:固定代码,现在可用

public void onCreate(Bundle savedInstanceState)
{
Log.v(TAG, "Activity State: onCreate()");
super.onCreate(savedInstanceState);
setContentView(R.layout.contact_manager);
mContactList = (ListView) findViewById(R.id.contactList);
populateContactList();

mContactList.setOnItemClickListener(new OnItemClickListener() {
String strid = Long.toString(id);
Cursor result = managedQuery(ContactsContract.Contacts.CONTENT_URI, null, ContactsContract.Contacts._ID +" = ?", new String[]{strid}, null);
if (result.moveToFirst()) {
Cursor c = getContentResolver().query(Data.CONTENT_URI,
new String[] {Data._ID, Phone.NUMBER, Phone.TYPE, Phone.LABEL},
Data.RAW_CONTACT_ID + "=?" + " AND "
+ Data.MIMETYPE + "='" + Phone.CONTENT_ITEM_TYPE + "'",
new String[] {String.valueOf(strid)}, null);
if(c.moveToFirst()){
int phoneColumn = c.getColumnIndex("data1");
String phoneNumber = c.getString(phoneColumn);
Log.d("DATA",phoneNumber);
}
}
});
}

编辑 1:忘记了一些重要的东西。代码改编自 Android 开发站点的 ContactManager 示例。

/**
* Populate the contact list based on account currently selected in the account spinner.
*/
private void populateContactList() {
// Build adapter with contact entries
Cursor cursor = getContacts();
String[] fields = new String[] {
ContactsContract.Data.DISPLAY_NAME
};
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.contact_entry, cursor,
fields, new int[] {R.id.contactEntryText});
mContactList.setAdapter(adapter);
}

/**
* Obtains the contact list for the currently selected account.
*
* @return A cursor for for accessing the contact list.
*/
private Cursor getContacts()
{
// Run query
Uri uri = ContactsContract.Contacts.CONTENT_URI;
String[] projection = new String[] {
ContactsContract.Contacts._ID,
ContactsContract.Contacts.DISPLAY_NAME
};
String selection = null;
String[] selectionArgs = null;
String sortOrder = ContactsContract.Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC";

return managedQuery(uri, projection, selection, selectionArgs, sortOrder);
}

最佳答案

假设您有一个有效的 contactID,您可以这样做:

Cursor result = managedQuery(ContactsContract.Contacts.CONTENT_URI, null, 
ContactsContract.Contacts._ID +" = ?",
new String[]{contactID}, null);
if (result.moveToFirst()) {

for(int i=0; i< result.getColumnCount(); i++){
Log.i("CONTACTSTAG", result.getColumnName(i) + ": "
+ result.getString(i));
}
}

您必须将 ContactsContract.Contacts.CONTENT_URI 和 where 子句更改为您正在查询的表。上面的代码将打印出一堆关于联系人的一般信息。

关于android - 从联系人 ID : android 中检索电话号码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8805937/

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