gpt4 book ai didi

Android 设备联系人显示重复的联系人条目

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:04:02 24 4
gpt4 key购买 nike

我打算开发一个 android 应用程序,以方便获取设备联系人并显示在列表中。

我正在使用以下代码获取设备联系人,它工作正常但显示重复的联系人条目。

//FETCH DEVICE CONTACTS
public void fetchDeviceContacts(){

Constant.progressDialog = ProgressDialog.show(ContactListActivity.this,"", "Please wait...");

Uri contactsUri = ContactsContract.Contacts.CONTENT_URI;
// Querying the table ContactsContract.Contacts to retrieve all the contacts
final Cursor contactsCursor = getContentResolver().query(contactsUri, null, null, null,
ContactsContract.Contacts.DISPLAY_NAME + " ASC ");

if(contactsCursor.moveToFirst()){
do{
long contactId = contactsCursor.getLong(contactsCursor.getColumnIndex("_ID"));

Uri dataUri = ContactsContract.Data.CONTENT_URI;

// Querying the table ContactsContract.Data to retrieve individual items like
// home phone, mobile phone, work email etc corresponding to each contact
Cursor dataCursor = getContentResolver().query(dataUri, null,
ContactsContract.Data.CONTACT_ID + "=" + contactId,
null, null);

String nickName="";
String homePhone="";
String mobilePhone="";
String workPhone="";
byte[] photoByte=null;
String homeEmail="";
String workEmail="";
String companyName="";
String title="";

if(dataCursor.moveToFirst()){
// Getting Display Name
contactName= dataCursor.getString(dataCursor.getColumnIndex(ContactsContract.Data.DISPLAY_NAME ));
do{

// Getting NickName
if(dataCursor.getString(dataCursor.getColumnIndex("mimetype")).equals(ContactsContract.CommonDataKinds.Nickname.CONTENT_ITEM_TYPE))
nickName = dataCursor.getString(dataCursor.getColumnIndex("data1"));

// Getting Phone numbers
if(dataCursor.getString(dataCursor.getColumnIndex("mimetype")).equals(ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE)){
switch(dataCursor.getInt(dataCursor.getColumnIndex("data2"))){
case ContactsContract.CommonDataKinds.Phone.TYPE_HOME :
homePhone = dataCursor.getString(dataCursor.getColumnIndex("data1"));
break;
case ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE :
mobilePhone = dataCursor.getString(dataCursor.getColumnIndex("data1"));
break;
case ContactsContract.CommonDataKinds.Phone.TYPE_WORK :
workPhone = dataCursor.getString(dataCursor.getColumnIndex("data1"));
break;
}
}

// Getting EMails
if(dataCursor.getString(dataCursor.getColumnIndex("mimetype")).equals(ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE ) ) {
switch(dataCursor.getInt(dataCursor.getColumnIndex("data2"))){
case ContactsContract.CommonDataKinds.Email.TYPE_HOME :
emailId =homeEmail= dataCursor.getString(dataCursor.getColumnIndex("data1"));
break;
case ContactsContract.CommonDataKinds.Email.TYPE_WORK :
emailId=workEmail = dataCursor.getString(dataCursor.getColumnIndex("data1"));
break;
}
}

// Getting Organization details
if(dataCursor.getString(dataCursor.getColumnIndex("mimetype")).equals(ContactsContract.CommonDataKinds.Organization.CONTENT_ITEM_TYPE)){
companyName = dataCursor.getString(dataCursor.getColumnIndex("data1"));
title = dataCursor.getString(dataCursor.getColumnIndex("data4"));
}

// Getting Photo
if(dataCursor.getString(dataCursor.getColumnIndex("mimetype")).equals(ContactsContract.CommonDataKinds.Photo.CONTENT_ITEM_TYPE)){
photoByte = dataCursor.getBlob(dataCursor.getColumnIndex("data15"));

if(photoByte != null) {
Bitmap bitmap = BitmapFactory.decodeByteArray(photoByte, 0, photoByte.length);

// Getting Caching directory
File cacheDirectory = getBaseContext().getCacheDir();

// Temporary file to store the contact image
File tmpFile = new File(cacheDirectory.getPath() + "/wpta_"+contactId+".png");

// The FileOutputStream to the temporary file
try {
FileOutputStream fOutStream = new FileOutputStream(tmpFile);

// Writing the bitmap to the temporary file as png file
bitmap.compress(Bitmap.CompressFormat.PNG,100, fOutStream);

// Flush the FileOutputStream
fOutStream.flush();

//Close the FileOutputStream
fOutStream.close();

} catch (Exception e) {
e.printStackTrace();
}
photoPath = tmpFile.getPath();

}
}


//CREATE CONTACT LIST
_contactBean=new ContactBean();
if(contactName!=null){
_contactBean.setName(contactName);
}
if(photoPath!=null){
_contactBean.setPhotoPath(photoPath);
}if(emailId!=null){
_contactBean.setEmailId(emailId);
}
if(contactName!=null){
_contactArrayList.add(_contactBean);
}

//////////////////////

}while(dataCursor.moveToNext());
/*String details = "";

// Concatenating various information to single string
if(homePhone != null && !homePhone.equals("") )
details = "HomePhone : " + homePhone + "\n";
if(mobilePhone != null && !mobilePhone.equals("") )
details += "MobilePhone : " + mobilePhone + "\n";
if(workPhone != null && !workPhone.equals("") )
details += "WorkPhone : " + workPhone + "\n";
if(nickName != null && !nickName.equals("") )
details += "NickName : " + nickName + "\n";
if(homeEmail != null && !homeEmail.equals("") )
details += "HomeEmail : " + homeEmail + "\n";
if(workEmail != null && !workEmail.equals("") )
details += "WorkEmail : " + workEmail + "\n";
if(companyName != null && !companyName.equals("") )
details += "CompanyName : " + companyName + "\n";
if(title != null && !title.equals("") )
details += "Title : " + title + "\n";*/

// Adding id, display name, path to photo and other details to cursor

}

}while(contactsCursor.moveToNext());
}

runOnUiThread(new Runnable() {
public void run() {
Constant.progressDialog.cancel();
contactsCursor.close();
}
});
}

帮助将不胜感激。谢谢

最佳答案

您会看到重复的,因为同一个联系人可能属于不同的组。例如电子邮件帐户 1 和电子邮件帐户 2。
您可以在 http://developer.android.com/guide/topics/providers/contacts-provider.html 的子标题原始联系人数据来源 中获取更多信息。

关于Android 设备联系人显示重复的联系人条目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15401229/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com