在android中与电话簿联系

时间:2017-01-05 08:36:47

标签: java android arraylist

我有两个数组列表NumINdb(VARIABLES)和常见(VARIABLES)......我在新的Arraylist Finalarr(VARIABLE)中比较了Arraylist中的两个Arraylist和检索到的共同元素......

我将Arraylist传递给函数getPlayers(Finalarr).....下面是函数...

我在FinalArr Arraylist中有电话号码,想要他们的名字来自电话的联系簿/电话簿....我想将价值存储在Arraylist中......

GoalManager

...错误

 private ArrayList < DATA_CONTACT > getPlayers(ArrayList < String > filterVALUES) {
    ArrayList < DATA_CONTACT > players = new ArrayList < DATA_CONTACT > ();

    ContentResolver cr = getContext().getContentResolver();

    Cursor phoneCursor;
    for (int o = 0; o < filterVALUES.size(); o++) {

        phoneCursor = cr.query(
            ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.NUMBER + " = ?", new String[] {
                filterVALUES.get(o).toString()
            }, null);

        DATA_CONTACT p = null;
        while (phoneCursor.moveToNext()) {

            p = new DATA_CONTACT();
            final String phoneNumber = phoneCursor.getString(phoneCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
            String contact_display_name = phoneCursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));

            // filtered.add(phoneNumber.toString());

            p.setName(contact_display_name);
            p.setPhone(phoneNumber.toString());
            p.setImg(R.drawable.com_facebook_button_like_background);
        }
        phoneCursor.close();
        players.add(p);

    }
    return players; //Returns a List of Contact Matching the Arraylist..
}

2 个答案:

答案 0 :(得分:1)

这里的例外点(最有可能?):

cursor.getColumnIndex(...

并告诉您光标 null

您的意思是 phoneCursor 吗?

答案 1 :(得分:0)

如果您只想从数组列表中获取名称(我假设您的数组列表包含数字),则下面是根据联系号码返回名称的方法。

public static String getContactName(Context context, String phoneNumber) {
    ContentResolver cr = context.getContentResolver();
    Uri uri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(phoneNumber));
    Cursor cursor = cr.query(uri, new String[]{ContactsContract.PhoneLookup.DISPLAY_NAME}, null, null, null);
    if (cursor == null) {
        return null;
    }
    String contactName = null;
    if(cursor.moveToFirst()) {
        contactName = cursor.getString(cursor.getColumnIndex(ContactsContract.PhoneLookup.DISPLAY_NAME));
    }

    if(cursor != null && !cursor.isClosed()) {
        cursor.close();
    }
    return contactName;
}

不要忘记添加阅读联系人&#39;清单中的权限

相关问题