Simplecursoradapter java display images

Asked

Viewed 172 times

0

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

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" />

  • 1

    Specify a little more what you want, I won’t look at your code unless I know what to do and what your problem is.

  • 1

    Did you have an error? What happened when you implemented the code up there? Encountered some difficulty?

  • i want to display the image from the url (photo), from the bd

  • In Textview textFoto you want the image URL to appear and when you click on it the image will appear

1 answer

1


To display the photo, you need to exchange Textview for an Imageview.

If the image Bitmap is saved in the database, I believe it will work. The layout would look 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 id of the element:

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

Another alternative is to use the SimpleCursorAdapter.ViewBinder, the following link can help: https://stackoverflow.com/questions/8510335/simplecursoradapter-with-imageview-and-textview

Browser other questions tagged

You are not signed in. Login or sign up in order to post.