2011-12-14 14 views
10

si può avere un layout con un imageview e textview per una riga in un SimpleCursorAdapter con un listview?SimpleCursorAdapter con ImageView e TextView

questo sarebbe il layout

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

si può fare in SimpleCursorAdapter con un controllo ListView? quando avevo bisogno di immagini in un listview ho sempre usato un arrayadapter personalizzato ma mai con un cursore.

Come si imposta l'immagine se è possibile farlo?

risposta

23

Quando la vista da associare è una ImageView e non sono presenti ViewBinder associate SimpleCursorAdapter.bindView() chiamate setViewImage(ImageView, String). Per impostazione predefinita, il valore verrà considerato come una risorsa immagine . Se il valore non può essere utilizzato come risorsa immagine, il valore viene utilizzato come immagine Uri.

Se avete bisogno di filtrare in altri modi il valore recuperato dal database è necessario un ViewBinder da aggiungere alla ListAdapter come segue:

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; 
    } 
}); 
+5

... o override setViewImage – Selvin

0

Per espandere sulla risposta da @Francesco Vadicamo, questo è un fuctions che fa parte di un'attività più grande. L'ho diviso perché avevo bisogno di chiamarlo da più aree del codice. databaseHandler e listView sono definiti come variabili di classe e inizializzati in 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); 
} 
Problemi correlati