Android:我有一个联系人名单列表,如何获取相应的电子邮件和电话号码?

时间:2014-01-20 12:04:06

标签: java android listview android-listview

我的活动有一个列表视图,显示启用了多选的所有联系人。我还有一个过滤器来过滤显示结果。

在解决这个问题3天之后,我终于可以生成用户选择的所有显示名称的列表。

我现在的问题是如何获取列表中每个显示名称的相应电子邮件和电话号码?

更新: 我找到了解决方案..

    public String getNumber(String name,Context context){

    String number="";

    Uri uri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
    String[] projection    = new String[] {ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
            ContactsContract.CommonDataKinds.Phone.NUMBER};

    Cursor people = context.getContentResolver().query(uri, projection, null, null, null);

    int indexName = people.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME);
    int indexNumber = people.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);

    people.moveToFirst();
    do {
        String Name   = people.getString(indexName);
        String Number = people.getString(indexNumber);
        if(Name.equalsIgnoreCase(name)){return Number.replace("-", "");}
        // Do work...
    } while (people.moveToNext());


    if(!number.equalsIgnoreCase("")){return number.replace("-", "");}
    else return number;
}

public String getEmail(String name,Context context){

    String email="";

    Uri uri = ContactsContract.CommonDataKinds.Email.CONTENT_URI;
    String[] projection    = new String[] {ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
            ContactsContract.CommonDataKinds.Email.DATA};

    Cursor people = context.getContentResolver().query(uri, projection, null, null, null);

    int indexName = people.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME);
    int indexNumber = people.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA);

    people.moveToFirst();
    do {
        String Name   = people.getString(indexName);
        String mail = people.getString(indexNumber);
        if(Name.equalsIgnoreCase(name)){return mail;}
        // Do work...
    } while (people.moveToNext());


    if(!email.equalsIgnoreCase("")){return email;}
    else return email;
}

从那里得到它: Android get contact number using name

0 个答案:

没有答案