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
- Cómo obtener una posición de elemento de lista haciendo clic en el botón dentro de él?
- SimpleCursorAdapter vs CursorAdapter?
- SimpleCursorAdapter obsoleto en la versión 15 de la API?
- Android: ¿Cómo Requerir un Cursor para actualizar ListView después de eliminar la fila de la base de datos?
- ¿Usando SimpleCursorAdapter con Spinner?
<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?
- ListView vacío si SimpleCursorAdapter cerrado ()
- ¿Por qué cursorLoader no notificó cambios en los datos de origen?
- Android - Obtención de la lista de contactos con direcciones de calle, pero no con valores bajos como Skype, donde la dirección es sólo una ciudad y un estado
- Simple coursor adaptador con base de datos SQLite y custmize listview
- ¿Cómo establecer el valor predeterminado de Spinner por su valor en lugar de su posición?
- ¿Qué bandera usar en SimpleCursorAdapter?
- Agregar un separador de fecha para un SimpleCursorAdapter en Android
- Utilizar SimpleCursorAdapter.ViewBinder para cambiar el color de TextView
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); }
- Cuándo utilizar sincronizado en Java
- ¿Cómo detectar Region Enter / Exit para múltiples balizas usando AltBeacon android-beacon-library?