【发布时间】:2013-12-18 16:02:40
【问题描述】:
我试图在光标适配器的 bindView 方法中在我的 ListView 中设置图像,实际上整个数据(与联系人有关)是在数据库表中预取的。我查询此表以获取我的图像 uri(对于每个联系人,无论是否有图像,都有一个图像 Uri)。现在对于那些没有图像的联系人,我想显示默认图像。但是,我尝试使用以下代码,但是我的图像在 Uri 位置中没有图像的视图中被在 Uri 位置中具有数据的图像(其他联系人图像)中重复。
以下是我的代码:
@Override
public void bindView(View view, Context context, Cursor cursor) {
((TextView) view.getTag(R.id.textView1)).setText(cursor.getString(cursor.getColumnIndexOrThrow("Name")));
((TextView) view.getTag(R.id.textView2)).setText(cursor.getString(cursor.getColumnIndexOrThrow("BirthDate")));
((TextView) view.getTag(R.id.textView1)).setTypeface(tf);
((TextView) view.getTag(R.id.textView2)).setTypeface(tf);
String image = cursor.getString(cursor.getColumnIndexOrThrow("imageUri"));
Uri IMAGE_URI = Uri.parse(image);
InputStream stream = Contacts.openContactPhotoInputStream(context.getContentResolver(), IMAGE_URI);
if (stream == null) {
((ImageView) view.getTag(R.id.imageView1)).setBackgroundResource(R.drawable.arrow_01);
}
if(stream != null){
BufferedInputStream buf = new BufferedInputStream(stream);
Bitmap my_btmp = BitmapFactory.decodeStream(buf);
((ImageView) view.getTag(R.id.imageView1)).setImageBitmap(my_btmp);
}
}
有什么想法吗?
【问题讨论】:
-
解决这个问题的另一种方法是直接查询联系人,但我不确定它会对内存产生什么影响,以及列表中每一行的处理速度会有多慢。
标签: android