使用SimpleCustomAdapter在listview中加载图像

时间:2012-09-02 07:15:42

标签: android listview imageview

我有以下代码:

list_home = (ListView) findViewById (R.id.list_home);

    cursor2 = baza5.query("igralci", new String[] {"slika","ime", "priimek",    "pozicija","visina","_id"}, 
                "id_ekipe like " + id_izbrane_ekipe_domaci+" AND " +
                        "je_izbran like " + pomozni,
                null, null, null, null);

    adapter2 = new SimpleCursorAdapter(
                this, 
                R.layout.igralci_ekipe, 
                cursor2, 
                new String[] {"slika","ime","priimek","pozicija","visina","_id"}, 
                new int[] {R.id.slika_igralca,R.id.ime_igralca,R.id.priimek_igralca,R.id.pozicija_igralca,
                        R.id.visina_igralca,R.id.id});

    list_home.setAdapter(adapter2);

我的igralci_ekipe.xml:

<?xml version="1.0" encoding="utf-8"?>



<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"

    android:padding="8px">

    <ImageView

        android:id="@+id/slika_igralca"
        android:layout_gravity="left"
        android:layout_width="50dp"
        android:layout_height="50dp" />



    <TextView
        android:id="@+id/ime_igralca"
        android:layout_width="wrap_content"
        android:textColor="#0000ff"
        android:textSize="15dp"
        android:layout_height="wrap_content"
         android:layout_toRightOf="@id/slika_igralca" />



    <TextView
        android:id="@+id/priimek_igralca"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="#0000ff"
        android:textSize="15dp"
        android:layout_marginLeft="16px"
        android:layout_toRightOf="@id/ime_igralca" />

    <TextView
        android:id="@+id/pozicija_igralca"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="#0000ff"
        android:textSize="15dp"
        android:layout_marginLeft="16px"
        android:layout_toRightOf="@id/priimek_igralca"/>

        <TextView
        android:id="@+id/visina_igralca"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="#0000ff"
        android:textSize="15dp"
        android:layout_marginLeft="16px"
        android:layout_toRightOf="@id/pozicija_igralca"/>




    <TextView
        android:id="@+id/id"
        android:layout_width="wrap_content"
        android:textColor="#0000ff"
        android:textSize="1dp"
        android:layout_height="wrap_content"
        android:layout_marginLeft="16px"
        android:layout_toRightOf="@id/visina_igralca"/>

</RelativeLayout>

数据库字段slika(图片)是我在sdcard上的图片的字符串路径。

这样可以正常显示图片,但是当有3张或更多图片需要加载时,我会收到内存错误。

Is there a way I could use picture bitmap like this:

    int targetW = slika_igralca.getWidth();
    int targetH = slika_igralca.getHeight();

    // Get the dimensions of the bitmap
    BitmapFactory.Options bmOptions = new BitmapFactory.Options();
    bmOptions.inJustDecodeBounds = true;

    int photoW = bmOptions.outWidth;
    int photoH = bmOptions.outHeight;

    // Determine how much to scale down the image
    int scaleFactor = Math.min(photoW/targetW, photoH/targetH);

    // Decode the image file into a Bitmap sized to fill the View
    bmOptions.inJustDecodeBounds = false;
    bmOptions.inSampleSize = scaleFactor;
    bmOptions.inPurgeable = true;

    Bitmap bitmap = BitmapFactory.decodeFile(selectedImagePath, bmOptions);
    slika_igralca.setImageBitmap(bitmap);

然后作为字符串使用类似这样的东西:slika_igralca.setImageBitmap(bitmap);

1 个答案:

答案 0 :(得分:0)

我解决了这个问题,我只需要创建一个自定义类适配器

gregoradapter.java:

    package com.example.ultimate.basketball.stats;

import android.content.Context;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.support.v4.widget.CursorAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;



public class gregoradapter extends CursorAdapter {

@SuppressWarnings("deprecation")
gregoradapter(Context context, Cursor cursor) {
super(context, cursor);

mInflater = LayoutInflater.from(context);

mColField1 = cursor.getColumnIndex("slika");
mColField2 = cursor.getColumnIndex("priimek");
}

@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
View v = mInflater.inflate(R.layout.igralci_ekipe, parent, false);
return v;
}

@Override
public void bindView(View view, Context context, Cursor cursor) {
// show text value

ImageView slika = (ImageView) view.findViewById(R.id.slika_igralca);
int targetW = 20;
int targetH = 20;

// Get the dimensions of the bitmap
BitmapFactory.Options bmOptions = new BitmapFactory.Options();
bmOptions.inJustDecodeBounds = true;

int photoW = bmOptions.outWidth;
int photoH = bmOptions.outHeight;

// Determine how much to scale down the image
int scaleFactor = Math.min(photoW/targetW, photoH/targetH);

// Decode the image file into a Bitmap sized to fill the View
bmOptions.inJustDecodeBounds = false;
bmOptions.inSampleSize = scaleFactor;
bmOptions.inPurgeable = true;

Bitmap bitmap = BitmapFactory.decodeFile(cursor.getString(mColField1), bmOptions);
slika.setImageBitmap(bitmap);


// show icon
TextView iv = (TextView) view.findViewById(R.id.priimek_igralca);
int icon_kind = cursor.getInt(mColField2);

iv.setText(cursor.getString(mColField2));
}


LayoutInflater mInflater;

private int mColField1;
private int mColField2;

}

然后像这样使用:

gregoradapter adapter15= new gregoradapter(this, cursor2);