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

时间:2012-01-10 15:39:25

标签: android contacts

一旦从列表视图中点击了联系人姓名,我就会尝试获取联系人数据。根据下面的代码,我可以成功记录我的联系人的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);
}

1 个答案:

答案 0 :(得分:3)

假设您有一个有效的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子句更改为您要查询的表。上面的代码将打印出一堆关于联系人的一般信息。