2011-12-14 54 views
10

你可以通過imageviewtextview爲中的一行進行佈局查看嗎?帶有ImageView和TextView的SimpleCursorAdapter

這將是佈局

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

能將它SimpleCursorAdapter做一個列表視圖?當我需要在列表視圖中的圖像時,我總是使用自定義的數組適配器,但從來沒有使用遊標。

如果可以完成,我將如何設置圖像?

回答

23

當綁定視圖是ImageView並且不存在ViewBinder關聯SimpleCursorAdapter.bindView()調用setViewImage(ImageView, String)。 默認情況下,該值將被視爲圖像資源。如果該值不能用作圖像資源,則該值將用作圖像Uri

如果需要其他的方式來過濾從數據庫中檢索你需要一個ViewBinder添加到ListAdapter如下價值:

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

...或覆蓋setViewImage – Selvin 2011-12-14 20:05:07

0

爲了從@Francesco Vadicamo答案擴大,這是一個作爲更大活動的一部分的功能。我將它分開,因爲我需要從代碼的多個區域調用它。 databaseHandlerlistView被定義爲類變量並在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); 
} 
相關問題