如何从 DateBase 加载图像到 ListView 中?
我正在使用 SimpleCursorAdapter
填充我的 listview
。 通过使用 getView(),我将每个项目的图像设置为 imageView。
现在我的光标中只有一张图像(在第一行)。但在滚动时它会出现在随机位置。为什么会这样呢?
代码
public class MyListAdapter extends SimpleCursorAdapter{
private Cursor cur;
private Context context;
private SQLiteDatabase db;
public MyListAdapter (Context context, Cursor c, SQLiteDatabase db) {
super(context, R.layout.list_item, c, new String[] {}, new int[] {});
this.cur=super.getCursor();
this.context = context;
this.db=db;
}
private class ViewHolder {
TextView txtTitle, txtDate;
ImageView imgSm;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.list_item, null);
holder = new ViewHolder();
holder.txtTitle = (TextView) convertView.findViewById(R.id.txt_title);
holder.txtDate = (TextView) convertView.findViewById(R.id.txt_date);
holder.imgSm= (ImageView) convertView.findViewById(R.id.img_sm);
convertView.setTag(holder);
}
else {
holder = (ViewHolder) convertView.getTag();
}
this.cur.moveToPosition(position);
holder.txtTitle.setText(this.cur.getString(this.cur.getColumnIndex("title")));
holder.txtDate.setText(this.cur.getString(this.cur.getColumnIndex("date")));
byte[] blob = this.cur.getBlob(this.cur.getColumnIndex("img_sm"));
if (blob!=null) {
holder.imgSm.setImageBitmap(BitmapFactory.decodeByteArray(blob, 0, blob.length));
}
return convertView;
}
}
I am filling in my listview
with a SimpleCursorAdapter
.
By using getView()
I set the image to an imageView
for each item.
Now I only have 1 image (in the first row) in my cursor
. But on scroll it appears in random positions. Why it is so?
code
public class MyListAdapter extends SimpleCursorAdapter{
private Cursor cur;
private Context context;
private SQLiteDatabase db;
public MyListAdapter (Context context, Cursor c, SQLiteDatabase db) {
super(context, R.layout.list_item, c, new String[] {}, new int[] {});
this.cur=super.getCursor();
this.context = context;
this.db=db;
}
private class ViewHolder {
TextView txtTitle, txtDate;
ImageView imgSm;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.list_item, null);
holder = new ViewHolder();
holder.txtTitle = (TextView) convertView.findViewById(R.id.txt_title);
holder.txtDate = (TextView) convertView.findViewById(R.id.txt_date);
holder.imgSm= (ImageView) convertView.findViewById(R.id.img_sm);
convertView.setTag(holder);
}
else {
holder = (ViewHolder) convertView.getTag();
}
this.cur.moveToPosition(position);
holder.txtTitle.setText(this.cur.getString(this.cur.getColumnIndex("title")));
holder.txtDate.setText(this.cur.getString(this.cur.getColumnIndex("date")));
byte[] blob = this.cur.getBlob(this.cur.getColumnIndex("img_sm"));
if (blob!=null) {
holder.imgSm.setImageBitmap(BitmapFactory.decodeByteArray(blob, 0, blob.length));
}
return convertView;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是因为ListView回收...
ListView 仅创建可见项目以及其他一些项目。
如果滚动,不可见的项目将在不再可见时被重复使用。
您仅设置第一个图像(正如您所说,光标中只有第一行有 blob 数据)
因此,如果重新使用第一行,您将得到一行,其中图像已准备好设置为第一行。
所以使用
it becouse ListView recycling ...
ListView creating only visible items + few more.
if you scroll invisible items will be reused if they are no longer visible.
you setting only first image(as you said that only first row in cursor has blob data)
so if first row is resused you get a row with image allready setup to fist one.
so use
因为您的 ListView 基本上不知道它正在渲染哪个图像,并且当您上下滚动时,它会刷新自身并随机重新绘制图像。
您需要跟踪哪个图像属于哪个 ListView 项目才能实现此目的
通过使用该位置。
Because your ListView basicly doesnt know which image it's rendering and when you are scrolling up and down it's refreshing itself and redrawing the image randomly.
You need to keep track of which image belong to which ListView Item you can achieve this
by using the position.