相关疑难解决方法(0)

SimpleCursorAdapter中的图像

我试图用SimpleCursorAdapter一个ViewBinder来从数据库中获取的图像,并把它放到我的ListView项目视图.这是我的代码:

    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")); …
Run Code Online (Sandbox Code Playgroud)

android blob cursor indexoutofboundsexception android-viewbinder

6
推荐指数
2
解决办法
1万
查看次数

Android自定义SimpleCursorAdapter,带有来自文件的图像,带有数据库中的路径

要点:自定义适配器通过数据库中的文件路径间接获取文件资源.低效/记忆问题.您的意见要求.

对所有搜索,相关链接和有用主题的引用都在后面.

以下代码有效,但有几个因素值得关注.需要一些更有经验的眼睛,请建议改善或潜在的错误,以避免.应用不需要是内容提供商(仅限应用于本地的数据).有问题的ListView将非常轻量级,只有大约5到10个条目.(我遗漏了数据库的东西,因为它有效.)

概述:

  • DataBase包含一些文本和图像文件路径.- 好
  • 图像文件存储在设备上(SD卡/外部存储器,无论哪里).- 好

文件不在数据库中使得它与普通的SimpleCursorAdapter不同 - 必须拉动图像文件.添加了在填充listview之前将其制作成缩略图的开销.

如上所述,它很轻,但即使只有一两个条目,虚拟机也会出现问题.我怀疑这是与Bitmaps相关的所有内存抖动:

08-27 19:53:14.273: I/dalvikvm-heap(11900): Grow heap (frag case) to 4.075MB for 1228816-byte allocation
08-27 19:53:14.393: D/dalvikvm(11900): GC_CONCURRENT freed <1K, 5% free 4032K/4244K, paused 13ms+3ms, total 116ms
Run Code Online (Sandbox Code Playgroud)
/* myTextAndImageCursorAdapter.java */

import android.widget.SimpleCursorAdapter;

//import android.support.v4.widget.SimpleCursorAdapter;
import android.content.Context;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import android.database.Cursor;
import java.io.File;

import android.widget.ImageView;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import static android.media.ThumbnailUtils.extractThumbnail;


public class TextAndImageCursorAdapter extends SimpleCursorAdapter { 

    private Context context;
    private int layout;

    public TextAndImageCursorAdapter …
Run Code Online (Sandbox Code Playgroud)

data-binding android image android-cursoradapter

0
推荐指数
1
解决办法
5507
查看次数