2012-08-08 98 views
0

我的適配器如何將位圖圖像添加到ListAdapter?

ListAdapter adapter = new SimpleAdapter(this, contactList, 
    R.layout.news_list_adapter,new String[] {TAG_NAME, TAG_ADDRESS, R.drawable.news_en2}, 
    new int[] {R.id.newsHead, R.id.qisaIzzah ,R.id.newsFontImage}); 

我有我想要添加到列表中的位圖圖像,我能寫的,而不是R.drawable.news_en2

例子位

byte[] decodedString = Base64.decode(news.getString("newsimage"),Base64.DEFAULT); 
Bitmap decodedByte = BitmapFactory.decodeByteArray(decodedString,0,decodedString.length); 
+0

使用自定義的ListView。 – 2012-08-08 05:29:02

+0

請參閱此鏈接,http://www.androidhive.info/2012/02/android-custom-listview-with-image-and-text/ – rajeshwaran 2012-08-08 05:30:55

回答

0

通過擴展BaseAdapter類可以在ListView中輕鬆添加位圖圖像

@Override 
    public View getView(int position, View convertView, ViewGroup parent) {  

     View row = convertView; 
    // if it's not recycled, initialize some attributes 
     if (row == null) { 
      LayoutInflater li = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      row = li.inflate(R.layout.menucategory, null);   
      //imageView.setLayoutParams(new GridView.LayoutParams(85, 85));   
     } 
      // getCategoryItem   
      Category category = (Category) getItem(position); 
      // Get reference to ImageView 
      categoryIcon = (ImageView) row.findViewById(R.id.category_image); 
      //categoryIcon.setLayoutParams(new GridView.LayoutParams(85, 85)); 
      categoryIcon.setScaleType(ImageView.ScaleType.CENTER_CROP); 
      categoryIcon.setPadding(5, 5, 5, 5); 

      categoryName = (TextView) row.findViewById(R.id.category_name); 
      //Set category name 
      categoryName.setText(category.getCategoryName());   


      if(category.getCategoryPath() != null){    
        Bitmap bitmap = BitmapFactory.decodeFile(category.getCategoryPath()); 
        System.out.println("bitmap2:: "+ bitmap); 
        if(bitmap != null){ 
        categoryIcon.setImageBitmap(bitmap); 
        }    
      }        

     return row; 
    } 
0

您還必須定義可以採用位圖的佈局。在Mohammod Hossain發佈的上述示例中,R.layout.menucategory引用了該自定義佈局。

0

基本上,簡單的適配器會自動將一些資源ID或URI綁定到行佈局的圖像視圖。 但它不支持位圖。

這是一個問題,因爲每個必須管理位圖的人都知道我們經常不得不減小圖片的大小以防止outOfMemory異常。 但是,如果您想要將圖像添加到listView中,則只能提供URI才能縮小圖像的大小。所以這裏是解決方案:

我已經修改了simpleAdapter以便能夠處理位圖。 將此類添加到您的項目中,並用它代替simpleAdapter。 然後,不要爲圖像傳遞URI或ressourceId,而是傳遞一個Bitmap!

以下,對代碼:

import java.util.HashMap; 
import java.util.List; 
import java.util.Map; 

import android.content.Context; 
import android.graphics.Bitmap; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.Checkable; 
import android.widget.ImageView; 
import android.widget.SimpleAdapter; 
import android.widget.TextView; 



public class ExtendedSimpleAdapter extends SimpleAdapter{ 
    List<HashMap<String, Object>> map; 
    String[] from; 
    int layout; 
    int[] to; 
    Context context; 
    LayoutInflater mInflater; 
    public ExtendedSimpleAdapter(Context context, List<HashMap<String, Object>> data, 
      int resource, String[] from, int[] to) { 
     super(context, data, resource, from, to); 
     layout = resource; 
     map = data; 
     this.from = from; 
     this.to = to; 
     this.context = context; 
    } 


@Override 
public View getView(int position, View convertView, ViewGroup parent) { 
    mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    return this.createViewFromResource(position, convertView, parent, layout); 
} 

private View createViewFromResource(int position, View convertView, 
     ViewGroup parent, int resource) { 
    View v; 
    if (convertView == null) { 
     v = mInflater.inflate(resource, parent, false); 
    } else { 
     v = convertView; 
    } 

    this.bindView(position, v); 

    return v; 
} 


private void bindView(int position, View view) { 
    final Map dataSet = map.get(position); 
    if (dataSet == null) { 
     return; 
    } 

    final ViewBinder binder = super.getViewBinder(); 
    final int count = to.length; 

    for (int i = 0; i < count; i++) { 
     final View v = view.findViewById(to[i]); 
     if (v != null) { 
      final Object data = dataSet.get(from[i]); 
      String text = data == null ? "" : data.toString(); 
      if (text == null) { 
       text = ""; 
      } 

      boolean bound = false; 
      if (binder != null) { 
       bound = binder.setViewValue(v, data, text); 
      } 

      if (!bound) { 
       if (v instanceof Checkable) { 
        if (data instanceof Boolean) { 
         ((Checkable) v).setChecked((Boolean) data); 
        } else if (v instanceof TextView) { 
         // Note: keep the instanceof TextView check at the bottom of these 
         // ifs since a lot of views are TextViews (e.g. CheckBoxes). 
         setViewText((TextView) v, text); 
        } else { 
         throw new IllegalStateException(v.getClass().getName() + 
           " should be bound to a Boolean, not a " + 
           (data == null ? "<unknown type>" : data.getClass())); 
        } 
       } else if (v instanceof TextView) { 
        // Note: keep the instanceof TextView check at the bottom of these 
        // ifs since a lot of views are TextViews (e.g. CheckBoxes). 
        setViewText((TextView) v, text); 
       } else if (v instanceof ImageView) { 
        if (data instanceof Integer) { 
         setViewImage((ImageView) v, (Integer) data);        
        } else if (data instanceof Bitmap){ 
         setViewImage((ImageView) v, (Bitmap)data); 
        } else { 
         setViewImage((ImageView) v, text); 
        } 
       } else { 
        throw new IllegalStateException(v.getClass().getName() + " is not a " + 
          " view that can be bounds by this SimpleAdapter"); 
       } 
      } 
     } 
    } 
} 



private void setViewImage(ImageView v, Bitmap bmp){ 
    v.setImageBitmap(bmp); 
} 



} 

這個類完全一樣的原班(SimpleAdapter)