【发布时间】:2019-03-01 19:03:39
【问题描述】:
我想从联系人列表中获取 50 x 50 个联系人。
我尝试了for loop,但它随机给了我 10 个联系人。
如何通过 50 个联系人获取 50 个 .. 请帮帮我
我的代码:
ArrayList<Contact_Model> contactList = new ArrayList<Contact_Model>();
Uri uri = ContactsContract.Contacts.CONTENT_URI;
Cursor contactsCursor = getContentResolver().query(uri, null, null,
null, ContactsContract.Contacts.DISPLAY_NAME + " ASC "); // Return
if (contactsCursor.moveToFirst()) {
do {
long contctId = contactsCursor.getLong(contactsCursor.getColumnIndex("_ID"));
Uri dataUri = ContactsContract.Data.CONTENT_URI; // URI to get
Cursor dataCursor = getContentResolver().query(dataUri, null,
ContactsContract.Data.CONTACT_ID + " = " + contctId,
null, null);
// Strings to get all details
String displayName = "";
String mobilePhone = "";
String contactNumbers = "";
String cNumber = "";
String contactImage = "";
String imnage = "";
Cursor phonesCursor = null;
Person.Urls urls;
try {
Uri phoneUri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode("Phone number"));
phonesCursor = context.getContentResolver().query(phoneUri, new String[]{ContactsContract.PhoneLookup.PHOTO_THUMBNAIL_URI}, null, null, null);
} catch (NullPointerException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
}
if (dataCursor.moveToFirst()) {
try {
imnage = dataCursor.getString(1);
Log.e("detail", "==============" + imnage);
} catch (NullPointerException e) {
e.printStackTrace();
}
}
if (dataCursor.moveToFirst()) {
displayName = dataCursor.getString(dataCursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));// get
do {
for (i = 0; i < 50; i++) {
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:
break;
case ContactsContract.CommonDataKinds.Phone.TYPE_WORK:
break;
case ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE:
contactNumbers = dataCursor.getString(dataCursor.getColumnIndex("data1"));
contactNumbers += mobilePhone;
int id = 1;
if (String.valueOf(contactNumbers).charAt(0) == '+') {
if (contactNumbers.length() == 13) {
String trim_num = contactNumbers.substring(3);
cNumber = trim_num;
cNumber = cNumber.replaceAll(" ", "");
Log.d("number", cNumber);
}
} else {
cNumber = contactNumbers;
cNumber = cNumber.replaceAll(" ", "");
Log.d("without + number", cNumber);
}
// break;
}
}
}
break;
} while (dataCursor.moveToNext()); // Now move to next
if (!cNumber.equals("")) {
contact = new Contact();
contact.setContctId(String.valueOf(contctId));
contact.setContactNumber(cNumber);
contact.setContactName(displayName);
contact.setContactImg(imnage);
contact.save();
contactList.add(new Contact_Model(displayName, cNumber, imnage));// Finally add
} else {
Log.d("Contact : ", "Don't add empty contact");
}
}
} while (contactsCursor.moveToNext());
}
我也试过 LIMIT 50 光标。 但是当我使用 LIMIT 时没有可用的联系人
Cursor contactsCursor = getContentResolver().query(uri,
null, null, null, "LIMIT 10, " + count);
count += 10;
【问题讨论】:
-
首先在您的列表中获取所有联系人,然后逐个迭代列表,每个列表包含 50 个项目,这就是您的逻辑
-
同时获取所有联系人,然后按字母顺序对其进行排序,然后开始逐个迭代 50 个联系人
-
当我删除 for 循环时,它会按字母顺序为我提供所有联系人@Quicklearner
-
这是获取设备上所有联系人数据的低效方式,需要直接查询数据表,示例代码见stackoverflow.com/a/50468411/819355
标签: android contacts do-while android-contacts