0

私は次のコードを持っています:

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(picture)は、SDカード上の私の画像の文字列パスです。

これは動作し、画像は正しく表示されますが、ロードする画像が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);

4

1 に答える 1

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);
于 2012-09-04T07:08:18.430 に答える