在SimpleCursorAdapter图片 [英] Images in SimpleCursorAdapter

查看:478
本文介绍了在SimpleCursorAdapter图片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用 SimpleCursorAdapter ViewBinder 来从数据库中获取的图像,并把它进入我的的ListView 项目视图。这是我的code:

I'm trying to use a SimpleCursorAdapter with a ViewBinder to get an image from the database and put it into my ListView item view. Here is my code:

    private void setUpViews() {
    mNewsView = (ListView) findViewById(R.id.news_list);

    Cursor cursor = getNews();
    SimpleCursorAdapter curAdapter = new SimpleCursorAdapter(
            getApplicationContext(), R.layout.cursor_item, cursor,
            new String[] { "title", "content", "image" },
            new int[] { R.id.cursor_title, R.id.cursor_content,
                    R.id.news_image });

    ViewBinder viewBinder = new ViewBinder() {

        public boolean setViewValue(View view, Cursor cursor,
                int columnIndex) {
            ImageView image = (ImageView) view;
            byte[] byteArr = cursor.getBlob(columnIndex);
            image.setImageBitmap(BitmapFactory.decodeByteArray(byteArr, 0, byteArr.length));
            return true;
        }
    };
    ImageView image = (ImageView) findViewById(R.id.news_image);
    viewBinder.setViewValue(image, cursor, cursor.getColumnIndex("image"));
    curAdapter.setViewBinder(viewBinder);
    mNewsView.setAdapter(curAdapter);
}

我得到一个:

I am getting a :

android.database.CursorIndexOutOfBoundsException: Index -1 requested, with a size of 60

在执行字节[] byteArr = cursor.getBlob(参数:columnIndex); 。有没有人有一个想法,我究竟做错了什么?

while executing byte[] byteArr = cursor.getBlob(columnIndex);. Does anyone have an idea what am I doing wrong?

推荐答案

我觉得 cursor.moveToFirst()还没有被调用,所以光标抛出 android.database.CursorIndexOutOfBoundsException。

I think the cursor.moveToFirst() has not been called so the cursor is throwing android.database.CursorIndexOutOfBoundsException.

使用光标之前您应经常检查是光标是空的或没有通过调用 cursor.moveToFirst()。这也将在第一位置定位光标。

Before using a cursor you should always check is the cursor is empty or not by calling cursor.moveToFirst(). This will also position the cursor at the first position.

这篇关于在SimpleCursorAdapter图片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆