SimpleCursorAdapter java display images

0

How do I display images in layout with SimpleCursorAdapter? My .java code:

protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.listarordens);

    SQLiteDatabase db   = openOrCreateDatabase("ordens.db", Context.MODE_PRIVATE, null);
    ListView ltwLocais  = (ListView)findViewById(R.id.listHist);
    Cursor cursor       = db.rawQuery("SELECT * FROM ordens ORDER BY _id ASC",null);

    String[] from = {"_id","numero","descricao","datahora","foto"};
    int[] to = {R.id.textId, R.id.textNumero, R.id.textDescricao,R.id.textDatahora,R.id.textFoto};



    @SuppressWarnings("deprecation")
    SimpleCursorAdapter ad = new SimpleCursorAdapter(getBaseContext(), R.layout.model_listar, cursor, from, to);

    ltwLocais.setAdapter(ad);
    db.close();


}

and my .xml:

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

<TextView
    android:id="@+id/textNumero"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Large Text"
    android:textAppearance="?android:attr/textAppearanceLarge" />

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

    <TextView
        android:id="@+id/textDescricao"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/textNumero"
        android:layout_marginTop="14dp"
        android:layout_weight="0.77"
        android:text="Medium Text"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    </LinearLayout>

<TextView
    android:id="@+id/textDatahora"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Small Text"
    android:textAppearance="?android:attr/textAppearanceSmall" />

<TextView
    android:id="@+id/textFoto"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/textDatahora"
    android:layout_toLeftOf="@+id/textNumero"
    android:text="Medium Text"
    android:textAppearance="?android:attr/textAppearanceMedium" />

<TextView
    android:id="@+id/textId"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/textFoto"
    android:layout_below="@+id/textFoto"
    android:text="Large Text"
    android:textAppearance="?android:attr/textAppearanceSmall"
    android:visibility="gone" />

    
asked by anonymous 06.05.2014 / 03:22

1 answer

1

To display the photo, you need to swap TextView with an ImageView.

If the Bitmap of the image is saved in the database, I believe it will work. The layout looks like this:

//Restante do layout...
<TextView
android:id="@+id/textDatahora"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Small Text"
android:textAppearance="?android:attr/textAppearanceSmall" />

<ImageView
android:id="@+id/imageFoto"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textDatahora"
android:layout_toLeftOf="@+id/textNumero"
android:textAppearance="?android:attr/textAppearanceMedium" />

And in your class, change the element id:

int[] to = {R.id.textId, R.id.textNumero, R.id.textDescricao,R.id.textDatahora,R.id.imageFoto};

Alternatively, you can use SimpleCursorAdapter.ViewBinder , the following link can help: link

    
08.05.2014 / 16:36