获取所有类型的(单个)联系
即时取得联系所有类型的单一联系人我成功但是当打印内部列表(多种类型的电话号码包含)时,它根据该单一联系人的内部列表的大小打印最后添加的联系人.....(没看过例子)
示例
for ex:联系人姓名:ABC类型数3移动电话:123,主页:456,办公室:789这个代码
for (int i = 0; i < numberlist.size(); i++) { Log.e("inner", numberlist.get(i).getName() + " number " + numberlist.get(i).getNumber()); }
将打印(三次但相同的联系号码)(内部,ABC号码789);
pojo class
public class ContactClass extends RealmObject {
@PrimaryKey
String id;
String name;
String number;
RealmList<ContactClass> numberlist;
public String getNumber() {
return number;
}
public void setNumber(String number) {
this.number = number;
}
public ContactClass() {
}
public RealmList<ContactClass> getNumberlist() {
return numberlist;
}
public void setNumberlist(RealmList<ContactClass> numberlist) {
this.numberlist = numberlist;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
我想要的结果
我想保存联系人姓名和该单个姓名的所有类型的数字..
我尝试过的代码
public void loadContact() {
//swipeRefreshLayout.setRefreshing(true);
RealmList<ContactClass> list = new RealmList<>();
PhoneNumberUtil phoneNumberUtil = PhoneNumberUtil.getInstance();
ContentResolver cr = getActivity().getContentResolver();
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
if (cur.getCount() > 0) {
while (cur.moveToNext()) {
RealmList<ContactClass> numberlist = new RealmList<>();
String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
String name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
ContactClass contactClass = new ContactClass();
contactClass.setId(id);
contactClass.setName(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));
int type = pCur.getInt(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE));
// Log.e("called", phoneNo + " " + name + " " + id);
switch (type) {
case ContactsContract.CommonDataKinds.Phone.TYPE_HOME:
try {
if (phoneNo.equals("") || phoneNo != null) {
contactClass.setNumber(phoneNo);
Log.e("test", phoneNo + " name " + name+" type "+"home");
numberlist.add(contactClass);
}
} catch (Exception e) {
Log.d(TAG, e.toString());
}
break;
case ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE:
try {
if (phoneNo.equals("") || phoneNo != null) {
contactClass.setNumber(phoneNo);
Log.e("test", phoneNo + " name " + name+" type "+"mobile");
numberlist.add(contactClass);
}
} catch (Exception e) {
Log.d(TAG, e.toString());
}
break;
case ContactsContract.CommonDataKinds.Phone.TYPE_WORK:
try {
if (phoneNo.equals("") || phoneNo != null) {
contactClass.setNumber(phoneNo);
Log.e("test", phoneNo + " name " + name+" type "+"work");
numberlist.add(contactClass);
}
} catch (Exception e) {
Log.d(TAG, e.toString());
}
break;
case ContactsContract.CommonDataKinds.Phone.TYPE_FAX_HOME:
try {
if (phoneNo.equals("") || phoneNo != null) {
contactClass.setNumber(phoneNo);
Log.e("test", phoneNo + " name " + name+" type "+"eaxhome");
numberlist.add(contactClass);
}
} catch (Exception e) {
Log.d(TAG, e.toString());
}
break;
case ContactsContract.CommonDataKinds.Phone.TYPE_FAX_WORK:
try {
if (phoneNo.equals("") || phoneNo != null) {
contactClass.setNumber(phoneNo);
Log.e("test", phoneNo + " name " + name+" type "+"faxwork");
numberlist.add(contactClass);
}
} catch (Exception e) {
Log.d(TAG, e.toString());
}
break;
case ContactsContract.CommonDataKinds.Phone.TYPE_MAIN:
try {
if (phoneNo.equals("") || phoneNo != null) {
contactClass.setNumber(phoneNo);
Log.e("test", phoneNo + " name " + name+" type "+"main");
numberlist.add(contactClass);
}
} catch (Exception e) {
Log.d(TAG, e.toString());
}
break;
case ContactsContract.CommonDataKinds.Phone.TYPE_OTHER:
try {
if (phoneNo.equals("") || phoneNo != null) {
contactClass.setNumber(phoneNo);
Log.e("test", phoneNo + " name " + name+" type "+"home");
numberlist.add(contactClass);
}
} catch (Exception e) {
Log.d(TAG, e.toString());
}
break;
case ContactsContract.CommonDataKinds.Phone.TYPE_CUSTOM:
try {
if (phoneNo.equals("") || phoneNo != null) {
contactClass.setNumber(phoneNo);
Log.e("test", phoneNo);
numberlist.add(contactClass);
}
} catch (Exception e) {
Log.d(TAG, e.toString());
}
break;
case ContactsContract.CommonDataKinds.Phone.TYPE_PAGER:
try {
if (phoneNo.equals("") || phoneNo != null) {
contactClass.setNumber(phoneNo);
Log.e("test", phoneNo);
numberlist.add(contactClass);
}
} catch (Exception e) {
Log.d(TAG, e.toString());
}
break;
}
}
pCur.close();
/* HashSet<ContactClass> h = new HashSet<>();
h.addAll(numberlist);
numberlist.clear();
numberlist.addAll(h);*/
contactClass.setNumberlist(numberlist);
list.add(contactClass);
for (int i = 0; i < numberlist.size(); i++) {
Log.e("inner", numberlist.get(i).getName() + " number " + numberlist.get(i).getNumber());
}
}//1637
}
// saveToRealm(list);
/* try {
HashSet hs = new HashSet();
hs.addAll(list); // demoArrayList= name of arrayList from which u want to remove duplicates
List<ContactClass> classList = new ArrayList<>();
list.clear();
classList.addAll(hs);
saveToRealm(classList);
Log.e("resize", classList.size() + "");
} catch (Exception e) {
Log.d("savetorealm", "catch " + e.toString());
}*/
}
}
答案 0 :(得分:0)
如果联系人有2个或更多电话号码,您将为同一个联系人创建多个ContactClass
。
尝试将主循环更改为:
// this loop iterates once per contact
while (cur.moveToNext()) {
if (cur.getInt(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER)) > 0) {
RealmList<ContactClass> numberlist = new RealmList<>();
String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
String name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
// create a new ContactClass just once per contact
ContactClass contactClass = new ContactClass();
contactClass.setId(id);
contactClass.setName(name);
Cursor pCur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?",
new String[]{id}, null);
// this loop iterates once per phone for each contact
while (pCur.moveToNext()) {
String phoneNo = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
int type = pCur.getInt(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE));
Log.e("called", phoneNo + " " + name + " " + id);
switch (type) {
...
}
}
pCur.close();
// add the new ContactClass just once per contact to list
contactClass.setNumberlist(numberlist);
list.add(contactClass);
for (int i = 0; i < numberlist.size(); i++) {
Log.e("numbersize", numberlist.get(i).getNumber() + numberlist.get(i).getName() + numberlist.size() + "");
}
}
}