安卓得到ArrayList中的所有联系人的电话号码 [英] Android get all contacts telephone number in ArrayList

查看:300
本文介绍了安卓得到ArrayList中的所有联系人的电话号码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想保存的所有联系人的电话号码在一个ArrayList但我不能找到一种方法如何。有没有办法让他们,而不是挑选他们一个个ContactsContract?

解决方案

  ContentResolver的CR = mContext.getContentResolver(); //活动/应用android.content.Context
    光标光标= cr.query(ContactsContract.Contacts.CONTENT_URI,NULL,NULL,NULL,NULL);
    如果(cursor.moveToFirst())
    {
        ArrayList的<字符串> alContacts =新的ArrayList<字符串>();
        做
        {
            字符串ID = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));

            if(Integer.parseInt(cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0)
            {
                光标pCur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,ContactsContract.CommonDataKinds.Phone.CONTACT_ID +=?,新的String [] {ID},NULL);
                而(pCur.moveToNext())
                {
                    串contactNumber = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                    alContacts.add(contactNumber);
                    打破;
                }
                pCur.close();
            }

        }而(cursor.moveToNext());
    }
 

I am trying to save all contacts telephone numbers in an ArrayList but I cant find a way how. Is there a way to get them instead of picking them one by one with ContactsContract?

解决方案

ContentResolver cr = mContext.getContentResolver(); //Activity/Application android.content.Context
    Cursor cursor = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
    if(cursor.moveToFirst())
    {
        ArrayList<String> alContacts = new ArrayList<String>();
        do
        {
            String id = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));

            if(Integer.parseInt(cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0)
            {
                Cursor pCur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?",new String[]{ id }, null);
                while (pCur.moveToNext()) 
                {
                    String contactNumber = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                    alContacts.add(contactNumber);
                    break;
                }
                pCur.close();
            }

        } while (cursor.moveToNext()) ;
    }

这篇关于安卓得到ArrayList中的所有联系人的电话号码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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