我想列出具有联系人姓名和联系电话号码的联系人列表,并希望确保名称应该有单一条目,以避免重复。这是我正在使用的代码:
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;
}
}
问题是我一次又一次地收到一个条目。我不知道我在哪里犯了错误。请帮帮我。谢谢。
答案 0 :(得分: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);
}
答案 1 :(得分:1)
我正在以这种方式使用&amp;为什么你在循环中使用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();