如何从 DateBase 加载图像到 ListView 中?

发布于 2024-12-15 15:24:27 字数 1937 浏览 0 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦幻的味道 2024-12-22 15:24:27

这是因为ListView回收...
ListView 仅创建可见项目以及其他一些项目。
如果滚动,不可见的项目将在不再可见时被重复使用。
您仅设置第一个图像(正如您所说,光标中只有第一行有 blob 数据)
因此,如果重新使用第一行,您将得到一行,其中图像已准备好设置为第一行。

所以使用

        if (blob!=null) {
            holder.imgSm.setImageBitmap(BitmapFactory.decodeByteArray(blob, 0, blob.length));
        }else{
           holder.imgSm.setImageResource(0);
        }

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

        if (blob!=null) {
            holder.imgSm.setImageBitmap(BitmapFactory.decodeByteArray(blob, 0, blob.length));
        }else{
           holder.imgSm.setImageResource(0);
        }
无远思近则忧 2024-12-22 15:24:27

因为您的 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文