【问题标题】:how to retrieve the list of contacts in android如何在android中检索联系人列表
【发布时间】:2013-04-20 19:01:23
【问题描述】:

我检索联系人列表(姓名、号码)的目的记录在一个 json 对象中,以通过 Web 服务发送到服务器

然后我找到了访问联系人的代码,很好,我测试了这段代码

String id, name;

        ContentResolver crs = getContentResolver();

        Cursor people = crs.query(ContactsContract.Contacts.CONTENT_URI, null,
                null, null, null);



        String phone = "";

        people.moveToPosition(Contexts.getnbContacts());

        while (people.moveToNext())

        {

            id = people.getString(people
                    .getColumnIndex(ContactsContract.Data._ID));
            name = people.getString(people
                    .getColumnIndex(ContactsContract.Data.DISPLAY_NAME));

            if (Integer
                    .parseInt(people.getString(people
                            .getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {

                Cursor phones = getContentResolver().query(
                        ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                        null,
                        ContactsContract.CommonDataKinds.Phone.CONTACT_ID
                                + " = " + id, null, null);
                while (phones.moveToNext()) {
                    // pdata =
                    // phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DATA));
                    phone = phone
                            + phones.getString(phones
                                    .getColumnIndex(ContactsContract.CommonDataKinds.Phone.DATA))
                            + " and ";
                    ;
                }

                phones.close();

            }

我的问题是:如何将每个联系人(姓名、号码)保存在数组或数组列表中...

每个表格行指定联系人

我认为数组类型兼容json格式

那么如何将联系人复制到 json 对象中

【问题讨论】:

标签: android json contacts


【解决方案1】:

这是我的代码

public static Map<String, String> getAddressBook(Context context)
{
    Map<String, String> result = new HashMap<String, String>();
    Cursor cursor = context.getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null);
    while(cursor.moveToNext()) 
    {
        int phone_idx = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
        int name_idx = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME);
        String phone = cursor.getString(phone_idx);
        String name = cursor.getString(name_idx);
        result.put(name, phone);
    }
    cursor.close();

    return result;
}

需要

<uses-permission android:name="android.permission.READ_CONTACTS"/>

【讨论】:

    【解决方案2】:

    您可以使用以下查询从联系人数据库中检索最喜欢的联系人

    Cursor cursor = this.managedQuery(ContactsContract.Contacts.CONTENT_URI, projection, "starred=?",new String[] {"1"}, null);
    
    猜你喜欢
    • 2012-03-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多