2011-12-14 44 views
10

bạn có thể có bố cục với imageviewtextview cho một hàng trong một SimpleCursorAdapter với chế độ xem danh sách không?SimpleCursorAdapter với ImageView và TextView

này sẽ được bố trí

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

nó có thể được thực hiện trong SimpleCursorAdapter với một listview? khi tôi cần hình ảnh trong một listview tôi luôn luôn sử dụng một mảng tùy chỉnh nhưng không bao giờ với một con trỏ.

Tôi làm cách nào để đặt hình ảnh nếu nó có thể được thực hiện?

Trả lời

23

Khi chế độ xem để ràng buộc là ImageView và hiện không có ViewBinder liên kết SimpleCursorAdapter.bindView() gọi setViewImage(ImageView, String). Theo mặc định, giá trị sẽ được coi là tài nguyên hình ảnh . Nếu không thể sử dụng giá trị làm tài nguyên hình ảnh, giá trị được sử dụng làm hình ảnh Uri.

Nếu bạn cần phải lọc theo những cách khác giá trị lấy từ cơ sở dữ liệu bạn cần một ViewBinder để thêm vào ListAdapter như sau:

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

... hoặc ghi đè setViewImage – Selvin

0

Mở rộng trên câu trả lời từ @Francesco Vadicamo, đây là một đấu giá là một phần của hoạt động lớn hơn. Tôi chia nó ra vì tôi cần gọi nó từ nhiều khu vực của mã. databaseHandlerlistView được định nghĩa là biến lớp và được khởi tạo trong 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); 
} 
Các vấn đề liên quan