SimpleCursorAdapter con ImageView y TextView

Puede tener un diseño con una imageview y textview para una fila en un SimpleCursorAdapter con un listview?

Este sería el diseño

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <ImageView android:id="@+id/icon" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:id="@+id/bowler_txt" android:paddingLeft="25dp" android:textSize="30dp" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Bowler" android:textAppearance="?android:attr/textAppearanceLarge" /> </LinearLayout> 

Se puede hacer en SimpleCursorAdapter con un listview? Cuando necesitaba imágenes en un listview siempre usaba un arrayadapter personalizado pero nunca con un cursor.

¿Cómo puedo configurar la imagen si se puede hacer?

Cuando la vista para enlazar es un ImageView y no hay asociadas a SimpleCursorAdapter.bindView() asociadas SimpleCursorAdapter.bindView() llamadas setViewImage(ImageView, String) . De forma predeterminada, el valor se tratará como un recurso de imagen . Si el valor no puede utilizarse como un recurso de imagen, el valor se utiliza como una imagen Uri .

Si necesita filtrar de otras formas el valor recuperado de la base de datos necesita un ViewBinder para agregar al ListAdapter siguiente manera:

 listAdapter.setViewBinder(new SimpleCursorAdapter.ViewBinder(){ /** Binds the Cursor column defined by the specified index to the specified view */ public boolean setViewValue(View view, Cursor cursor, int columnIndex){ if(view.getId() == R.id.your_image_view_id){ //... ((ImageView)view).setImageDrawable(...); return true; //true because the data was bound to the view } return false; } }); 

Para ampliar la respuesta de @Francesco Vadicamo, se trata de una función que es parte de una actividad más grande. Lo dividí porque necesitaba llamar desde múltiples áreas del código. listView y listView se definen como variables de clase y se inicializan en onCreat() .

 private void updateListView() { // Get a Cursor with the current contents of the database. final Cursor cursor = databaseHandler.getCursor(); // The last argument is 0 because no special behavior is required. SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.listview, cursor, new String[] { databaseHandler.ICON, databaseHandler.BOWLER_TXT }, new int[] { R.id.icon, R.id.bowler_txt }, 0); // Override the handling of R.id.icon to load an image instead of a string. adapter.setViewBinder(new SimpleCursorAdapter.ViewBinder() { public boolean setViewValue(View view, Cursor cursor, int columnIndex) { if (view.getId() == R.id.imageview) { // Get the byte array from the database. byte[] iconByteArray = cursor.getBlob(columnIndex); // Convert the byte array to a Bitmap beginning at the first byte and ending at the last. Bitmap iconBitmap = BitmapFactory.decodeByteArray(iconByteArray, 0, iconByteArray.length); // Set the bitmap. ImageView iconImageView = (ImageView) view; iconImageView.setImageBitmap(iconBitmap); return true; } else { // Process the rest of the adapter with default settings. return false; } } }); // Update the ListView. listView.setAdapter(adapter); } 
  • Enfoque para rellenar la vista de lista expandible con la base de datos SQlite local
  • ListView no se actualiza al agregar a SQLiteDatabase
  • Causado por: java.lang.IllegalArgumentException: columna '_id' no existe
  • Implementación de una vista de lista con selección múltiple con filtro utilizando un adaptador de cursor
  • Android: ¿Cómo poner un archivo de imagen de la tarjeta SD a HashMap con simpleadapter?
  • AutoCompleteTextView con CursorLoader y SimpleCursorAdapter
  • Uso de la vista de reciclaje con una base de datos
  • Cursor se desactiva antes de llamar a este método
  • Usando simpleCursorAdapter
  • Android: Utilizar SimpleCursorAdapter para obtener datos de la base de datos a ListView
  • Android, usando SimpleCursorAdapter para establecer el color no sólo las cadenas
  • FlipAndroid es un fan de Google para Android, Todo sobre Android Phones, Android Wear, Android Dev y Aplicaciones para Android Aplicaciones.