我正在制作一个应用程序,以便在列表视图中显示联系人号码,但它提供重复的联系人条目我想只显示一个联系人
private void getContactList() {
ContentResolver cr = getContentResolver();
Cursor cur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + " ASC");
if ((cur != null ? cur.getCount() : 0) > 0) {
while (cur != null && cur.moveToNext()) {
String id = cur.getString(
cur.getColumnIndex(ContactsContract.Contacts._ID));
String name = cur.getString(cur.getColumnIndex(
ContactsContract.Contacts.DISPLAY_NAME));
if (cur.getInt(cur.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 phoneNo = pCur.getString(pCur.getColumnIndex(
ContactsContract.CommonDataKinds.Phone.NUMBER));
ContactsPlacer obj = new ContactsPlacer();
obj.setContactname(name);
obj.setContactnumber(phoneNo);
names.add(obj);
}
pCur.close();
}
}
}
if(cur!=null){
cur.close();
}
}
帮我解释一下重复的联系人没有在listview中显示
答案 0 :(得分:0)
试试这个
if(email!=null){
if(!names.contains(name)){
names.add(name);
}
}
答案 1 :(得分:0)
这里使用像这样的arraylist的contains方法
public ArrayList<String> getNameEmailDetails(){
ArrayList<String> names = new ArrayList<String>();
ContentResolver cr = getContentResolver();
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,null, null, null, null);
if (cur.getCount() > 0) {
while (cur.moveToNext()) {
String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
Cursor cur1 = cr.query(
ContactsContract.CommonDataKinds.Email.CONTENT_URI, null,
ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = ?",
new String[]{id}, null);
while (cur1.moveToNext()) {
//to get the contact names
String name=cur1.getString(cur1.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
Log.e(TAG+"Name :", name);
String email = cur1.getString(cur1.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
Log.e(TAG+"Email", email);
if(email!=null){
if(!names.contains(name))
names.add(name);
}
}
cur1.close();
}
}
return names;
}
答案 2 :(得分:0)
boolean checkexsistance(ContactsPlacer obj)
{
boolean test= true;
for(int i = 0 ; i<names.size();i++)
{
if(names.get(i).getContactname().equals(obj.getContactname()))
{
test = false;
break;
}
}
return test;
}
private void getContactList() {
ContentResolver cr = getContentResolver();
Cursor cur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + " ASC");
if ((cur != null ? cur.getCount() : 0) > 0) {
while (cur != null && cur.moveToNext()) {
String id = cur.getString(
cur.getColumnIndex(ContactsContract.Contacts._ID));
String name = cur.getString(cur.getColumnIndex(
ContactsContract.Contacts.DISPLAY_NAME));
if (cur.getInt(cur.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 phoneNo = pCur.getString(pCur.getColumnIndex(
ContactsContract.CommonDataKinds.Phone.NUMBER));
ContactsPlacer obj = new ContactsPlacer();
obj.setContactname(name);
obj.setContactnumber(phoneNo);
if(checkexsistance(obj))
names.add(obj);
}
pCur.close();
}
}
}
if(cur!=null){
cur.close();
}
}
答案 3 :(得分:0)
前几天我遇到了同样的问题,并使用此代码解决了同样的问题, 请试一试,如果需要进一步的帮助,请告诉我。
别忘了在舱单中要求许可和提及 的&#34; android.permission.READ_CONTACTS&#34; 强>
ContentResolver cr = getActivity().getContentResolver();
String[] PROJECTION = new String[]{ContactsContract.RawContacts._ID,
ContactsContract.Contacts.DISPLAY_NAME,
ContactsContract.Contacts.PHOTO_ID,
ContactsContract.CommonDataKinds.Email.DATA,
ContactsContract.CommonDataKinds.Phone.NUMBER,
ContactsContract.RawContacts.VERSION};
String order = "CASE WHEN "
+ ContactsContract.Contacts.DISPLAY_NAME
+ " NOT LIKE '%@%' THEN 1 ELSE 2 END, "
+ ContactsContract.Contacts.DISPLAY_NAME
+ ", "
+ ContactsContract.CommonDataKinds.Phone.DATA
+ " COLLATE NOCASE";
String filter = ""+ ContactsContract.Contacts.HAS_PHONE_NUMBER + " > 0 and " + ContactsContract.CommonDataKinds.Phone.TYPE +"=" + ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE;
Cursor cur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, PROJECTION, filter, null, order);
for (int i = 0; i < cur.getColumnCount(); i++) {
Timber.e("column " + i + "=" + cur.getColumnName(i));
}
if (cur.moveToFirst()) {
do {
String name = cur.getString(1);
String number = cur.getString(4);
number = Utils.removeExtraCharFromString(number);
if (number.length() > 8) {
mContactNumbers = mContactNumbers + number + ",";
PhoneContacts phoneContacts = new PhoneContacts();
try {
phoneContacts.setName(URLEncoder.encode(name, "UTF-8"));
phoneContacts.setNumber(number);
phoneContacts.setNameInitial(String.valueOf(phoneContacts.getName().charAt(0)));
listOfContactsRaw.add(phoneContacts);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
} while (cur.moveToNext());
}
cur.close();
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
Set set = new TreeSet(new Comparator() {
@Override
public int compare(Object o1, Object o2) {
PhoneContacts phoneContacts1 = (PhoneContacts) o1;
PhoneContacts phoneContacts2 = (PhoneContacts) o2;
if (phoneContacts1.getNumber().equalsIgnoreCase(phoneContacts2.getNumber())) {
return 0;
}
return 1;
}
});
set.addAll(listOfContactsRaw);
System.out.println("\n***** After removing duplicates *******\n");
listOfContacts = new ArrayList(set);
// Set listOfContacts with your recycler view
}
}, 200);