Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/350.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/sql-server-2008/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在android中从电话簿获取联系人_Java_Android_Arraylist - Fatal编程技术网

Java 在android中从电话簿获取联系人

Java 在android中从电话簿获取联系人,java,android,arraylist,Java,Android,Arraylist,我有两个数组列表NumINdb(变量)和common(变量)…我比较了两个Arraylist,并在一个新的Arraylist Finalarr(变量)中从Arraylist检索了common元素 我正在将Arraylist传递给函数getPlayers(Finalarr)…下面是函数 我在FinalArr Arraylist中有电话号码,希望他们的姓名来自电话的联系人簿/电话簿…我希望将值存储在Arraylist中 private ArrayList < DATA_CONTACT >

我有两个数组列表NumINdb(变量)和common(变量)…我比较了两个Arraylist,并在一个新的Arraylist Finalarr(变量)中从Arraylist检索了common元素

我正在将Arraylist传递给函数getPlayers(Finalarr)…下面是函数

我在FinalArr Arraylist中有电话号码,希望他们的姓名来自电话的联系人簿/电话簿…我希望将值存储在Arraylist中

 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..
}
此处的例外点(最有可能?):

并告诉您,光标


您是指phoneCursor吗?

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

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;
}

别忘了在清单中添加“阅读联系人”权限

我不明白您想从我们这里得到什么?我们为你写代码?意思:请不要只谈论你想做什么;但请解释是什么阻碍了你到达那里。此外:你希望我们花时间帮助你;因此,请花1分钟来正确格式化/缩进源代码。长话短说:在这里花了一些时间,阅读了这里要问什么/如何问。@ghostCAT我已经把我的错误信息放在这里了..我得到..请帮助..这更有意义(尽管如此:为什么堆栈跟踪的格式设置很慢)。。。不管怎样:从这里开始阅读:我想要帮助我的问题,我已经通过了…请检查。。。变量contact\u display\u name给出的是空错误…我完全幻想这个问题的小写。。。
cursor.getColumnIndex(...
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;
}