[英]Best way to load a large amount of data in android without making the user wait
我正在读取电话联系人并在我的应用程序(通话记录,收藏夹联系人和所有联系人)中使用它们的应用程序中工作。
我的UI由选项卡宿主控件组成,用户可以在它们之间交换,因为我希望我的数据可以在所有活动中共享并且也只能保存在一个位置。
因此,我创建了一个称为数据控制器的单例类,当我打开应用程序时,我将显示加载屏幕,直到所有数据加载完毕。
现在的问题是,由于用户在有大量联系人的情况下每次打开应用程序时都等待大量时间(大约1分钟),因此用户在抱怨,那么如何才能以一种好的方式优化代码?
编辑
这是我用来获取所有联系人的方法:
public static ArrayList<ContactInfo> getAllContactWithNumberAndNameAndPhoto(
Context context, boolean starred) {
ArrayList<ContactInfo> retList = new ArrayList<ContactInfo>();
ContentResolver cr = context.getContentResolver();
Cursor cur = null;
if (starred == true) {
cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null,
"starred=?", new String[] { "1" }, null);
} else {
cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null,
null, null);
}
if (cur.getCount() > 0) {
while (cur.moveToNext()) {
ContactInfo item = new ContactInfo();
String id = cur.getString(cur
.getColumnIndex(ContactsContract.Contacts._ID));
String name = cur
.getString(cur
.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
Uri photo = PhoneUtils.getPhotoUriFromID(context, id);
String starredValue = cur.getString(cur
.getColumnIndex(ContactsContract.Contacts.STARRED));
boolean isFav = false;
if (starredValue.equals("1"))
isFav = true;
if (Integer
.parseInt(cur.getString(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));
item.addPhone(removeCharactersFromPhoneNumber(phoneNo));
}
pCur.close();
if (photo != null) {
item.setPhoto(photo.toString());
}
item.setName(name);
item.setFavorite(isFav);
item.setRecent(false);
retList.add(item);
}
}
cur.close();
}
return retList;
}
如果可以优化此方法,请告诉我。
我很惊讶从设备加载触点要花费这么长时间。
您是否配置了应用程序的配置文件以查看实际花费的时间? 这里似乎有些问题。
如果确实需要很长的时间从系统提供程序加载(由于操作系统),则可以缓存结果(即放入您自己的SQL数据库中),这样就可以在每次应用访问时快速加载(<1秒)并从设备刷新在后台。
我想您的方法的瓶颈是照片加载。 尝试加载除照片之外的所有内容,然后显示您的活动,同时加载照片。
您也可以尝试创建自己的应用程序表,其中仅包含所需的数据。 因此,在加载联系人时您将减少选择。 但是您将必须同步表。 您可以同时进行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.