获取数组中的联系人列表 [英] get contact list in an array

查看:41
本文介绍了获取数组中的联系人列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Cursor c;
        ArrayList<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>();
        c=getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,null,null,ContactsContract.Contacts.DISPLAY_NAME+" ASC ");
        int i=0;
        while (c.moveToNext()){
            //get contact list
            String diplayName=c.getString(c.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
            String number=c.getString(c.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));

            //put value into Hashmap
            HashMap<String, String> user_data = new HashMap<>();
            user_data.put(Constant.NAME, diplayName);
            user_data.put(Constant.PHONE_NUMBER, number);
            list.add(user_data);


//            String[] arr = { "1", "2", "3" };
//            System.out.println(Arrays.toString(arr)); // prints "[1, 2, 3]"
//            arr = append(arr, "4");
//            System.out.println(Arrays.toString(arr)); // prints "[1, 2, 3, 4]"
            String[] name = new String []{c.getString(c.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME))};
            Log.d("CONTACTS_NAME", name[0]);
        }

        Log.d("CONTACTS_NAME_ARRAY_LIST", String.valueOf(list));

        c.close();

我从 CONTACTS_NAME

04-15 11:39:52.333 7623-7623/com.game.stakes D/CONTACTS_NAME: B

04-15 11:39:52.333 7623-7623/com.game.stakes D/CONTACTS_NAME: Bs

但是,输出不是很有帮助..所以,我尝试了

But, the output isn't very helpful.. So, I tried

String[] name = new String []{};
while (c.moveToNext()){
     .....
.  .. . ..
 .. . 

name[i] = displayName;
i++;
}

这次我只是出错

java.lang.ArrayIndexOutOfBoundsException: length=0; index=0

我以为我可以计算联系人列表c.getColumnIndex(ContactsContract.CommonDataKinds.Phone._ID).但是,我尝试了很多方法来计算它们都不起作用.

I thought I could count contact list c.getColumnIndex(ContactsContract.CommonDataKinds.Phone._ID). But, I tried lot of way to count none of them are working.

我的问题是如何将 displayName 添加到单个数组中?

My question is how can I add displayName to a single array?

我不想要任何关于 ArrayList 的答案.因为,我已经使用过它,我想将它们添加到 Array 而不是 ArrayList

I don't want any answer on ArrayList. Cause, I have already worked with it and, I want to add them to Array not ArrayList

推荐答案

String[] name = new String [10000]{};
while (c.moveToNext()){
     .....
.  .. . ..
 .. . 

name[i] = displayName;
i++;
}

Log.d("COUNTS_CONTACTS", Arrays.toString(name));

输出:

[B, Bs, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, .....]

[B, Bs, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, .....]

我实际上并没有声明数组大小.所以,当我添加数组大小时.然后,再次打印出来它工作正常.

I wasn't actually declaring array size. So, when I added array size. Then, print out again it was working fine.!

这篇关于获取数组中的联系人列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆