본문 바로가기
IT 실무/모바일 프로그래밍

SimpleAdapter로 listView에 이미지 띄우기

by 지식id 2013. 11. 16.
반응형

그냥 SimpleAdapter는 Bitmap을 지원하지 않아서 이미지를 처리하기가 까다롭다.

다음 코드를 그대로 복사해서 ExtendedSimpleAdapter 라는 class파일로 만들고, SimpleAdapter를 ExtendedSimpleAdapter 로 바꾸어 준다.



package your.pakage;

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);
	}
}



출처 : http://stackoverflow.com/questions/6327465/displaying-bitmap-image-in-imageview-by-simple-adapter/

반응형

댓글