Wednesday, December 26, 2012

Binding bitmap to ImageView with SimpleAdapter

SimpleAdapter does not bind bitmaps to ImageView. That's the bottom line. From viewing the source of SimpleAdapter it seems it only binds text and integers (???) to ImageView.

I didn't like the extension I've found which do that (it overrides the whole binding function instead of just enhancing it). so I wrote my own.

package com.utils;

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

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

public class SimpleAdapterEx extends SimpleAdapter {
 private int mResource;
    private int[] mTo;
    private String[] mFrom;

    private List<? extends Map<String, ?>> mData;
 
 public SimpleAdapterEx(Context context, List<? extends Map<String, ?>> data,
            int resource, String[] from, int[] to) {
  super(context, data, resource, from, to);
  
  mResource = resource;
  mData = data;
  mTo = to;
  mFrom = from;
 }

 @Override
    public View getView(int position, View convertView, ViewGroup parent) {
  View v = super.getView(position, convertView, parent);

  v = createViewFromResourceEx(v, position, convertView, parent, mResource);

  return v;
    }
 
 @Override
    public View getDropDownView(int position, View convertView, ViewGroup parent) {
  View v = super.getDropDownView(position, convertView, parent);
  
  v = createViewFromResourceEx(v, position, convertView, parent, mResource);
  
  return v;
 }
 
    private View createViewFromResourceEx(View v, int position, View convertView,      
            ViewGroup parent, int resource) {
     
        bindView(position, v);

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

        final ViewBinder binder = getViewBinder();
        final String[] from = mFrom;
        final int[] to = mTo;
        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 ImageView) {
                        if (data instanceof Bitmap) {
                            setViewImage((ImageView) v, (Bitmap)data);                            
                        }                        
                    }
                }
            }
        }     
    }      
    
    private void setViewImage(ImageView imageView, Bitmap bitmap) {
     imageView.setImageBitmap(bitmap);
    }
}

Friday, December 21, 2012

Android Market Optimization: Part II

Earlier this year I wrote about Android Market Optimization blog posts. Now there are few sites that can help you do that:
In general you should follow ASO tag in your favorite tech blog, for example on TechCrunch.