2011-12-14 19 views
10

के साथ SimpleCursorAdapter क्या आपके पास imageview और textview के साथ एक सूची दृश्य के साथ SimpleCursorAdapter में एक पंक्ति के लिए लेआउट हो सकता है?ImageCiew और TextView

इस लेआउट होगा

<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

0

@Francesco Vadicamo से जवाब पर विस्तार करने के लिए, यह एक है एक बड़ी गतिविधि का हिस्सा है कि फ्यूक्शन। मैंने इसे विभाजित कर दिया क्योंकि मुझे इसे कोड के कई क्षेत्रों से कॉल करने की आवश्यकता थी। databaseHandler और listView को कक्षा चर के रूप में परिभाषित किया गया है और 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); 
} 
संबंधित मुद्दे