在 Android 中获得不同联系人的最佳方式是啥
Posted
技术标签:
【中文标题】在 Android 中获得不同联系人的最佳方式是啥【英文标题】:What is the best way to get distinct contacts in Android在 Android 中获得不同联系人的最佳方式是什么 【发布时间】:2014-11-06 05:37:06 【问题描述】:我通过此代码成功地将联系人存储在 parse.com 仪表板数据浏览器中。
public void readContacts()
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));
String name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
if (Integer.parseInt(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) ==1)
System.out.println(name );
ParseObject testObject = new ParseObject("Contacts");
testObject.put("names", name);
// get the phone number
Cursor pCur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?",
new String[]id, null);
while (pCur.moveToNext())
String phone = pCur.getString(
pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
System.out.println( phone);
testObject.put("phonenumber", phone);
pCur.close();
testObject.saveInBackground();
但是没有检查重复的联系人!
它存储所有从 sim / 手机内存中复制的联系人。
如何避免?
我认为一种可能的方法是将不同的姓名(联系人)存储在本地数据库中,然后检索该数据以将其存储在 parse.com 中
有没有更好的办法?
提前谢谢...
【问题讨论】:
这里有什么问题?您是否收到重复条目? 是的,我收到的重复条目与我手机中存在的相同 【参考方案1】:一种简单的方法是将数据加载到没有重复数据的 MatrixCursor 中。例如,假设您有一个光标 c1 将有很多联系人,但您需要一个没有重复数据的光标。你可以这样做:
MatrixCursor mc = new MatrixCursor(new String[]
Phone._ID,
Phone.DISPLAY_NAME_PRIMARY,
Phone.NUMBER
);
String lastNumber = "";
while(c1.moveToNext())
String id = c1.getString(c1.getColumnIndexOrThrow(Phone._ID));
String name = c1.getString(c1.getColumnIndexOrThrow(Phone.DISPLAY_NAME_PRIMARY)));
String number = c1.getString(c1.getColumnIndexOrThrow(Phone.NUMBER));
//Some condition to check previous data is not matched and only then add row
if(!lastNumber.contains(number))
lastNumber = number;
mc.addRow(new String[]id, name, number);
c1.close();
创建一个具有相同列的 MatrixCursor 实例,如果最后一个号码或联系人名称与前一个联系人的不匹配,则加载。检查的条件由您决定。以某种顺序查询数据,以便重复的联系人首先保持在一起。
加载 MatrixCursor 后,您可以从中获取数据。您还可以通过自定义 CursorLoader 或 CursorAdapter 将其附加到视图。
【讨论】:
【参考方案2】:请看下面的方法。您将获得没有重复电话号码的联系人列表。
public void readContacts()
ContentResolver cr = getContentResolver();
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
ArrayList<ParseObject> contacts = new ArrayList<ParseObject>();
ArrayList<String> list = new ArrayList<String>();
if (cur.getCount() > 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))) == 1)
System.out.println(name);
ParseObject testObject = new ParseObject("Contacts");
testObject.put("names", name);
// get the phone number
Cursor pCur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?", new String[] id , null);
while (pCur.moveToNext())
String phone = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
System.out.println(phone);
testObject.put("phonenumber", phone);
if(!list.contains(phone))
contacts.add(testObject);
list.add(phone);
pCur.close();
testObject.saveInBackground();
【讨论】:
感谢您的回复!但是同样的问题存在,&你为什么要使用 ` if(!list.contains(phone)) contacts.add(testObject); list.add(phone);` 在 testObject.put(* 之后【参考方案3】:Set 是 java 中不允许重复的集合。您可以将数据放入以数字为键、名称为值的集合中,以避免重复数字。
稍后您可以将它们从集合中取回并放入您的 testObject 中,名称为键,数字为值。
【讨论】:
你能解释一下如何实现它吗?【参考方案4】:这是我为您制定的解决方案.... 你可以通过 logcat 了解它是如何工作的 100%
import java.util.ArrayList;
import android.app.Activity;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract.CommonDataKinds.Phone;
import android.provider.ContactsContract.Contacts;
public class MainActivity extends Activity
String ClsSimPhonename = null;
String ClsSimphoneNo = null;
public static ArrayList<String> phonecontact = new ArrayList<String>();
public static ArrayList<String> simcontact = new ArrayList<String>();
public static ArrayList<String> totalcontact = new ArrayList<String>();
public static ArrayList<String> repeatedcontact = new ArrayList<String>();
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// get phone contact...
getphonecontact();
// get sim contact...
getsimcard_contact();
System.out.println("phone??? " + phonecontact);
System.out.println("sim??? " + simcontact);
System.out.println("sim_size??? " + simcontact.size());
System.out.println("phone_size??? " + phonecontact.size());
System.out.println("totalcontact_size??? " + totalcontact.size());
// filter process beigins here....
nowFilterContact();
private void nowFilterContact()
// TODO Auto-generated method stub
// determine which contact have more item....
if (simcontact.size() > phonecontact.size())
onemorefiltermethod(simcontact.size(), simcontact, phonecontact);
else
onemorefiltermethod(phonecontact.size(), phonecontact, simcontact);
private void onemorefiltermethod(int size, ArrayList<String> contacts,
ArrayList<String> contact2)
// TODO Auto-generated method stub
// compare both contact and get repeated contacts....
for (int i = 0; i < size; i++)
try
if (contacts.contains(contact2.get(i)))
// add repeated contacts to array....
repeatedcontact.add(contact2.get(i));
catch (Exception e)
System.out.println("repeatedcontact_size??? " + repeatedcontact.size());
// now delete repeated contact from total contact
now_deletedrepeated_contact_from_total();
private void now_deletedrepeated_contact_from_total()
// TODO Auto-generated method stub
for (int i = 0; i < totalcontact.size(); i++)
try
if (totalcontact.contains(repeatedcontact.get(i)))
totalcontact.remove(repeatedcontact.get(i));
catch (Exception e)
System.out.println("Final contact size" + totalcontact.size());
System.out.println("Final contact " + totalcontact);
private void getsimcard_contact()
// TODO Auto-generated method stub
try
Uri simUri = Uri.parse("content://icc/adn");
Cursor cursorSim = this.getContentResolver().query(simUri, null,
null, null, null);
while (cursorSim.moveToNext())
ClsSimPhonename = cursorSim.getString(cursorSim
.getColumnIndex("name"));
ClsSimphoneNo = cursorSim.getString(cursorSim
.getColumnIndex("number"));
ClsSimphoneNo.replaceAll("\\D", "");
ClsSimphoneNo.replaceAll("&", "");
ClsSimPhonename = ClsSimPhonename.replace("|", "");
/*
* add contact from phone to array phone array and total array
*/
phonecontact.add(ClsSimphoneNo);
totalcontact.add(ClsSimphoneNo);
catch (Exception e)
e.printStackTrace();
private void getphonecontact()
// TODO Auto-generated method stub
try
String[] PROJECTION = new String[] Contacts._ID,
Contacts.DISPLAY_NAME, Phone.NUMBER ;
Cursor c = managedQuery(Phone.CONTENT_URI, PROJECTION, null, null,
null);
if (c.moveToFirst())
String ClsPhonename = null;
String ClsphoneNo = null;
do
ClsPhonename = c.getString(c
.getColumnIndex(Contacts.DISPLAY_NAME));
ClsphoneNo = c.getString(c.getColumnIndex(Phone.NUMBER));
/*
* add contact from sim to array sim array and total array
*/
simcontact.add(ClsphoneNo);
totalcontact.add(ClsphoneNo);
ClsphoneNo.replaceAll("\\D", "");
ClsPhonename = ClsPhonename.replaceAll("&", "");
ClsPhonename.replace("|", "");
String ClsPhoneName = ClsPhonename.replace("|", "");
while (c.moveToNext());
catch (Exception e)
权限
<uses-permission android:name="android.permission.READ_CONTACTS"/>
您的输出在now_deletedrepeated_contact_from_total()
方法上。
检查totalcontact
最终输出的数组值
【讨论】:
嗯,谢谢它的工作,但如果它在其他数组列表中也有名称可能会更好以上是关于在 Android 中获得不同联系人的最佳方式是啥的主要内容,如果未能解决你的问题,请参考以下文章
让Android主屏幕小部件在textviews之间交替的最佳方式是啥?
以编程方式(Kotlin)在Android中修改和显示联系人的最佳方式?