2

这是我的代码,通过单击列表项给我联系人的姓名和电话号码:

public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

    final String contactName = cursor.getString(cursor.getColumnIndexOrThrow("DISPLAY_NAME"));
    Cursor contCursor =(Cursor) listStarred.getItemAtPosition(position);
    String strid = contCursor.getString(contCursor.getColumnIndexOrThrow(ContactsContract.Contacts._ID));

    ContentResolver cr = getContentResolver();

    String name = null;
    String lname ="...";

    contCursor = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
            null,
            ContactsContract.CommonDataKinds.Phone.CONTACT_ID +"='"+strid+"'",
            null, null);

    int phoneNumberIndex = contCursor
            .getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Phone.NUMBER);
    Log.d("Count", String.valueOf(contCursor.getCount()));

    if (contCursor != null) {
        Log.v("CurNotNull", "Cursor Not null");
        try {
            if (contCursor.moveToNext()) {
                Log.v("MoveToFirst", "Moved to first");
                Log.v("CheckMTF", "Cursor Moved to first and checking");
                lname = contCursor.getString(phoneNumberIndex);
                Toast.makeText(getApplicationContext(), contactName + " " + lname, Toast.LENGTH_SHORT).show();
            }
        } finally {
            Log.v("Finally", "In finally");
            contCursor.close();
        }
}
}

上面的代码似乎可以正常工作,但是如何不获取一个电话号码来获取包含所有联系人号码的列表呢?需要进行哪些更改才能获得包含所选联系人的号码(和每个类型)的数组?

4

1 回答 1

0

回答的用户删除了他的答案,但他实际上帮助我使用了他/她提供的代码。我能够使用提供的部分代码获得其他数字。所以这里是改进的代码:

@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

    final String contactName = cursor.getString(cursor.getColumnIndexOrThrow("DISPLAY_NAME"));
    contCursor =(Cursor) listStarred.getItemAtPosition(position);
    String strid = contCursor.getString(contCursor.getColumnIndexOrThrow(ContactsContract.Contacts._ID));

    ContentResolver cr = getContentResolver();

    ArrayList<String> phones = new ArrayList<String>();
    contCursor = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
            null,
            ContactsContract.CommonDataKinds.Phone.CONTACT_ID +"='"+strid+"'",
            null, null);

    while (contCursor.moveToNext()){
        String phoneNo = contCursor.getString(contCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
        Toast.makeText(getApplicationContext(), "Name: " + name + ", Phone No: " + phoneNo, Toast.LENGTH_SHORT).show();

    }
}

现在只需将数字保存在数组或列表中即可填充列表视图。

于 2013-06-08T14:51:50.003 回答