我正在使用游标获取联系人数据并尝试将其加载到ListView
中我根据QuickContact APIDemos样本(API级别8)开发了我的代码
这是我的代码,我已经修改了一下,但我面临性能问题 如果我删除了联系人照片代码,那么性能会很好,否则就像拖动列表一样太慢
适配器代码
private class ContactListAdapter extends ResourceCursorAdapter{
Cursor cur;
public ContactListAdapter(Context context, int layout, Cursor c) {
super(context, layout, c);
cur = c;
// TODO Auto-generated constructor stub
}
@Override
public void bindView(View view, Context arg1, Cursor arg2) {
// TODO Auto-generated method stub
final ContactListItemCache cache = (ContactListItemCache) view.getTag();
TextView nameView = cache.nameView;
QuickContactBadge photoView = cache.photoView;
cur.copyStringToBuffer(cur.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME), cache.nameBuffer);
int size = cache.nameBuffer.sizeCopied;
cache.nameView.setText(cache.nameBuffer.data, 0, size);
long contactId = cur.getLong(cur.getColumnIndex(ContactsContract.Contacts._ID));
Uri contactUri = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_URI, String.valueOf(contactId));
InputStream input = ContactsContract.Contacts.openContactPhotoInputStream(getContentResolver(), contactUri);
cache.photoView.setImageBitmap(BitmapFactory.decodeStream(input));
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
View view = super.newView(context, cursor, parent);
ContactListItemCache cache = new ContactListItemCache();
cache.nameView = (TextView) view.findViewById(R.id.name);
cache.photoView = (QuickContactBadge) view.findViewById(R.id.badge);
view.setTag(cache);
return view;
}
}
final static class ContactListItemCache {
public TextView nameView;
public QuickContactBadge photoView;
public CharArrayBuffer nameBuffer = new CharArrayBuffer(128);
}
}
获取联系人代码
private Cursor getContacts()
{
// Run query
Uri uri = ContactsContract.Contacts.CONTENT_URI;
String[] projection = new String[] {
ContactsContract.Contacts._ID,
ContactsContract.Contacts.DISPLAY_NAME,
ContactsContract.Contacts.LOOKUP_KEY
};
String selection = ContactsContract.Contacts.IN_VISIBLE_GROUP + " = '" +
(mShowInvisible ? "0" : "1") + "'";
String[] selectionArgs = null;
String sortOrder = ContactsContract.Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC";
return managedQuery(uri, projection, selection, selectionArgs, sortOrder);
}
注意: 我经历了这个线程,但我们都使用不同的方法 Load contact photo in a listview performance
修改
我已按照代码写下来但是调用了
imageDownloader.download(contactUri,
(ImageView) cache.photoView ,
getContentResolver(),
contactId);
下载图片代码
Bitmap downloadBitmap(Uri url) {
final int IO_BUFFER_SIZE = 4 * 1024;
InputStream inputStream = null;
try {
inputStream = ContactsContract.Contacts.openContactPhotoInputStream(resolver, url);
if(inputStream ==null){
return BitmapFactory.decodeStream(inputStream);
}else
{
return BitmapFactory.decodeStream(new FlushedInputStream(inputStream));
}
// return BitmapFactory.decodeStream(inputStream);
// Bug on slow connections, fixed in future release.
} finally {
if (inputStream != null) {
try {
inputStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
但问题是我正在使用这段代码,并在几秒后快速向下滚动 QuickBadgeContact小部件正在加载,之前没有任何内容出现在QuickBadgeContact的占位符中。
查看图片
有时图像会在很长时间后出现,请看这里
所以它不能正常工作联系ListView在android工作中,如果我们滚动得快得多,那么aslo QuickBadgeContact将默认存在并充当占位符,QuickBadgeContact内的图像稍后加载但在我的代码中整个徽章出现在几秒钟
我只需要继续将图片加载到有照片的联系人,并让其他人不动,因为这是加载黑色位图,其中没有为用户找到图像,但我不知道我该怎么做? /强>
答案 0 :(得分:3)
保持应用程序响应的简单规则是:不要在UI线程上执行I / O操作。 bindView()实现会破坏规则,因为它从数据库中读取位图。在慢速I / O操作完成时,主线程被阻塞。
您应该创建AsyncTasks来加载图像。最佳实践AFAIK是在您的应用程序中拥有一个完全可重用的ImageCache类......使用如下方法:
public static void getImage(Uri imageUri, ImageView imageView);
即。你给它一个URI和一个ImageView引用,它将负责在后台线程上下载图像并在它准备好时更新ImageView。
还有更多的东西......未完成的请求需要被取消,你应该在WeakReferences中保存ImageViews以避免泄漏它所属的Activity(有一个关于这个的Android Developer的博客文章)。
答案 1 :(得分:3)
我认为延迟加载是你的答案。 这是使用ListView的ScrollListener实现的。
在活动中它看起来像这样:
public class ListViewActivity extends Activity {
public boolean mBusy = false;
// even more initializing
@Override
public void onCreate(Bundle savedInstanceState) {
// The code to build the activity
ListView listView = findViewById(R.id.ListView);
listView.setOnScrollListener(listViewOnScrollListener);
listView.setAdapter(new ContactListAdapter(this, R.layout.whatsoever, null)
// it is important to give the Adapter 'this' as context because we need the mBusy boolean
// we should populate the adapter later in onCreate...
// even more code
}
private AbsListView.OnScrollListener listViewOnScrollListener = new AbsListView.OnScrollListener() {
@Override
public void onScrollStateChanged(AbsListView absListView, int scrollState) {
if (absListView instanceof ListView) {
ListView listView = (ListView)absListView;
switch (scrollState) {
case AbsListView.OnScrollListener.SCROLL_STATE_IDLE:
mBusy = false;
// Find out which elements of the ListView are visible to the user
int first = listView.getFirstVisiblePosition();
int childMargin = 0;
if (first < listView.getHeaderViewsCount()) {
first = listView.getHeaderViewsCount();
childMargin = listView.getHeaderViewsCount();
}
int last = listView.getLastVisiblePosition();
if (last > (listView.getCount() - listView.getFooterViewsCount() - 1)) {
last = listView.getCount() - listView.getFooterViewsCount() - 1;
}
for (int i = first; i <= last; i++) {
// This is for only populating the visible items of the list
// Action to perform the loading of your contactPhoto
// or displaying it
// maybe you call a method from the adapter to populate the views
adapter.loadImageBadges(listView.getChildAt(i - first + childMargin)),i);
}
break;
case AbsListView.OnScrollListener.SCROLL_STATE_TOUCH_SCROLL:
mBusy = true;
break;
case AbsListView.OnScrollListener.SCROLL_STATE_FLING:
mBusy = true;
break;
}
}
}
@Override
public void onScroll(AbsListView absListView, int i, int i1, int i2) {
// Do something when the list is scrolling
}
};
// The rest of the activity code
}
适配器将获得其他代码:
private class ContactListAdapter extends ResourceCursorAdapter {
public ContactListAdapter(Context context, int layout, Cursor c) {
super(context, layout, c);
cur = c;
}
@Override
public void bindView(View view, Context arg1, Cursor arg2) {
// TODO Auto-generated method stub
final ContactListItemCache cache = (ContactListItemCache) view.getTag();
TextView nameView = cache.nameView;
cur.copyStringToBuffer(cur.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME), cache.nameBuffer);
int size = cache.nameBuffer.sizeCopied;
cache.nameView.setText(cache.nameBuffer.data, 0, size);
if (!context.mBusy) {
// Do the expensive things only if the list is not scrolling...
loadImageBadges(convertView, arg2.getPosition())
} else {
// initialize with dummy data
}
}
public void ImageBadges(View view, int position) {
final ContactListItemCache cache = (ContactListItemCache) view.getTag();
QuickContactBadge photoView = cache.photoView;
long contactId = cur.getLong(cur.getColumnIndex(ContactsContract.Contacts._ID));
Uri contactUri = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_URI, String.valueOf(contactId));
InputStream input = ContactsContract.Contacts.openContactPhotoInputStream(getContentResolver(), contactUri);
cache.photoView.setImageBitmap(BitmapFactory.decodeStream(input));
}
// The rest of the adapter code
}
通过采用此代码,您将获得延迟加载,只会填充当前可见的视图,因此性能应该像您提到的那样增加。
另外一个有意义的方法是缓存光标加载时检索到的照片。
修改
上面提到的代码是在http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/view/List13.html - &gt;的Android示例中借用和采用的。缓慢的适配器
编辑2:
我同意Reuben Scratton将适配器的硬件工作部分(例如图像处理)放入另一个线程中。
答案 2 :(得分:0)
我和你有同样的问题,我找到了这个解决方案。您可以查看默认Android Contacts
中的ContactsPhotoLoader
。将mPhotoLoader.loadPhoto(contactAvatar, photoId);
放入getView
,contactAvatar
为ImageView
,photoID
为Contacts.PHOTO_ID
(根据联系方式)