如何从android中选择独特的联系人

我想从 Android只选择具有电话号码的联系人的唯一联系人.我正在使用此代码
ContentResolver cr = getContentResolver();
        Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,null,ContactsContract.Contacts.disPLAY_NAME);
        // Find the ListView resource.
        mainListView = (ListView) findViewById(R.id.mainListView);

        // When item is tapped,toggle checked properties of CheckBox and
        // Planet.
        mainListView
                .setonItemClickListener(new AdapterView.OnItemClickListener()
                {
                    public void onItemClick(AdapterView<?> parent,View item,int position,long id)
                    {
                        ContactsList planet = listadapter.getItem(position);
                        planet.toggleChecked();
                        PlanetViewHolder viewHolder = (PlanetViewHolder) item
                                .getTag();
                        viewHolder.getCheckBox().setChecked(planet.isChecked());
                    }
                });

        // Create and populate planets.
        planets = (ContactsList[]) getLastNonConfigurationInstance();
        // planets = new Planet[10];
        // planets.Add("asdf");
        ArrayList<ContactsList> planetList = new ArrayList<ContactsList>();
        String phoneNumber = null;
        String phoneType = null;

        count = cur.getCount();
        contacts = new ContactsList[count];

        if (planets == null)
        {
            if (cur.getCount() > 0)
            {
                planets = new ContactsList[cur.getCount()];
                int i = 0;
                //
                while (cur.movetoNext())
                {
                    String id = cur.getString(cur
                            .getColumnIndex(ContactsContract.Contacts._ID));
                    String name = cur
                            .getString(cur
                                    .getColumnIndex(ContactsContract.Contacts.disPLAY_NAME));
                    if (Integer
                            .parseInt(cur.getString(cur
                                    .getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0)
                    {
                        // Query phone here. Covered next
                        Cursor pCur = cr
                                .query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,ContactsContract.CommonDataKinds.Phone.CONTACT_ID
                                                + " = ?",new String[]
                                        { id },null);

                        // WHILE WE HAVE CURSOR GET THE PHONE NUMERS
                        while (pCur.movetoNext())
                        {
                            // Do something with phones
                            phoneNumber = pCur
                                    .getString(pCur
                                            .getColumnIndex(ContactsContract.CommonDataKinds.Phone.DATA));

                            phoneType = pCur
                                    .getString(pCur
                                            .getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE));

                            Log.i("Pratik",name + "'s PHONE :" + phoneNumber);
                            Log.i("Pratik","PHONE TYPE :" + phoneType);
                        }
                        pCur.close();
                    }

                    planets = new ContactsList[]
                    { new ContactsList(name,phoneNumber) };

                    contacts[i] = planets[0];
                    planetList.addAll(Arrays.asList(planets));

                    i++;
                }
            }

代码检索所有联系人并将其放入列表中.但我想要独特的联系,只有那些没有电话的人.我怎样才能做到这一点??有什么方法可以在查询中传递一些参数来只选择唯一的联系人???

解决方法

我想你的意思是你有一些联系人的重复记录.因此,您必须为查询添加条件.关键部分是联系人必须在可见组中并且有电话号码.
String selection = ContactsContract.Contacts.IN_VISIBLE_GROUP + " = '"
                + ("1") + "'";
        String sortOrder = ContactsContract.Contacts.disPLAY_NAME
                + " COLLATE LOCALIZED ASC";
cur = context.getContentResolver().query(
                ContactsContract.Contacts.CONTENT_URI,projection,selection
                        + " AND " + ContactsContract.Contacts.HAS_PHONE_NUMBER
                        + "=1",sortOrder);// this query only return contacts which had phone number and not duplicated

相关文章

Android性能优化——之控件的优化 前面讲了图像的优化,接下...
前言 上一篇已经讲了如何实现textView中粗字体效果,里面主要...
最近项目重构,涉及到了数据库和文件下载,发现GreenDao这个...
WebView加载页面的两种方式 一、加载网络页面 加载网络页面,...
给APP全局设置字体主要分为两个方面来介绍 一、给原生界面设...
前言 最近UI大牛出了一版新的效果图,按照IOS的效果做的,页...