0

我想列出具有联系人姓名和联系电话的联系人列表,并希望确保姓名应该有一个条目以避免重复。这是我正在使用的代码:

String[] projection    = new String[] {ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
                        ContactsContract.CommonDataKinds.Phone.NUMBER};

                m_cursorgetContacts = activity.getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,projection,null,null, null);

                if(m_cursorgetContacts != null && m_cursorgetContacts.getCount() != 0)
                {
                    while (m_cursorgetContacts.moveToNext())
                    {
                        String name = m_cursorgetContacts.getString(m_cursorgetContacts.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
                        String phoneNumber = m_cursorgetContacts.getString(m_cursorgetContacts.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));

                        if(name != null && phoneNumber != null && phoneNumber.length() != 0)
                        {
                            for(int l_a = 0;l_a < m_localContactNameHolderArrayList.size();l_a++)
                            {
                                InputProviderClass inputProviderInstance = (InputProviderClass)m_localContactNameHolderArrayList.get(l_a);              
                                if (!inputProviderInstance.l_contactName.equals(name) ) 
                                {
                                    l_inputProviderInstance.setContactName(name);
                                    l_inputProviderInstance.setContactNumber(phoneNumber);
                                    m_localContactNameHolderArrayList.add(l_inputProviderInstance);
                                }
                            }
                        }
                    }
                }
                else
                    Log.e("Info: ","There is no any contact present in the contact list of the phone");

                m_cursorgetContacts.close();

                for(int l_a = 0;l_a < m_localContactNameHolderArrayList.size();l_a++)
                {
                    Log.v("", m_localContactNameHolderArrayList.get(l_a).getContactName());
                }

这是我在数组列表中输入值的类:

static class InputProviderClass 
    {
        String l_contactName;
        String l_ContactNumber;

        public String getContactName()
        {
            return l_contactName;
        }
        public void setContactName(String contactName) 
        {
            this.l_contactName = contactName;
        }
        public String getContactNumber() 
        {
            return l_ContactNumber;
        }
        public void setContactNumber(String contactNumber) {
            this.l_ContactNumber = contactNumber;
        }
    }

问题是我一次又一次地得到一个条目。我不知道我在哪里犯了错误。请帮助我。提前致谢。

4

2 回答 2

1

我以这种方式使用&为什么你在while循环中使用for循环:--

  Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,   null,null,null, null);

        while(phones.moveToNext()){
             tempContactList=new ContactController();
            tempContactList.ContactId= phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
            tempContactList.PersonName=phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
            contactList.add(tempContactList);
            }
         phones.close(); 
于 2012-12-17T06:27:12.500 回答
1

尝试:

InputProviderClass inputProviderInstance = 
              (InputProviderClass)m_localContactNameHolderArrayList.get(l_a);              
    if (!inputProviderInstance.l_contactName.equals(name) ) 
        {
             inputProviderInstance=new inputProviderInstance();
             l_inputProviderInstance.setContactName(name);
             l_inputProviderInstance.setContactNumber(phoneNumber);
             m_localContactNameHolderArrayList.add(l_inputProviderInstance);
        }
于 2012-12-17T06:21:26.173 回答