`

ListView中显示SD卡、数据库、网络图片

 
阅读更多

实现下图,图片来自SD卡、数据库、网络图片


 

定义一个ListView的xml(res/layout/list_view.xml)

 

<?xml version="1.0" encoding="utf-8"?>
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
  	  android:id="@+id/h_list_view"
	  android:layout_width="fill_parent"
	  android:layout_height="fill_parent"
	/>
 

Activity代码

 

public class MessageListActivity extends Activity {
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.home);

		setViews();
	}
	private void setViews() {		
		ListView lv = (ListView)findViewById(R.id.h_list_view);
		SimpleAdapter adapter = new ImageSimpleAdapter(this, getDatas() ,R.layout.message_list
				, new String[]{"icon","title","shortContent"}, new int[]{R.id.ml_icon,R.id.ml_title,R.id.ml_short_content});
		lv.setAdapter(adapter);
		lv.setOnItemClickListener(new OnItemClickListener(){
			@Override
			public void onItemClick(AdapterView<?> av, View v, int position, long id) {
				//处理列表项的点击事件
			}
		});
	}
	private List<Map<String,Object>> getDatas() {
		List<Map<String,Object>> list = new ArrayList<Map<String,Object>>();
		for (int i=1; i<=12; i++) {
			Map<String,Object> map = new HashMap<String,Object>();
			String imagePath = "/sdcard/image_0"+i+".jpg";
			if (i >= 10) imagePath = "/sdcard/image_"+i+".jpg";
			map.put("icon", BitmapFactory.decodeFile(imagePath));
			map.put("title", "My Title "+i);
			map.put("shortContent", "my short content "+i);
			list.add(map);
		}
		return list;
	}
}
 

 

SimpleAdapter中能放置drawable中的图片,如果想放置其它来源的图片(如:SD卡、数据库、网络图片),那么需要重写它的bindView方法,这是个private方法,因此还需要重写所以调用它的方法。

 

package com.example.adapter;

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 ImageSimpleAdapter extends SimpleAdapter {
	private int[] mTo;
    private String[] mFrom;
    private ViewBinder mViewBinder;
    private List<? extends Map<String, ?>> mData;
    private int mResource;
    private int mDropDownResource;
    private LayoutInflater mInflater;
	public ImageSimpleAdapter(Context context, List<? extends Map<String, ?>> data, int resource, String[] from, int[] to) {
		super(context, data, resource, from, to);
		mTo = to;
		mFrom = from;
		mData = data;
		mResource = mDropDownResource = resource;
		mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}
	public View getView(int position, View convertView, ViewGroup parent) {
        return createViewFromResource(position, convertView, parent, mResource);
    }
    @Override
    public View getDropDownView(int position, View convertView, ViewGroup parent) {
        return createViewFromResource(position, convertView, parent, mDropDownResource);
    }
	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;
        }

        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 = mViewBinder;
        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 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");
                    }
                }
            }
        }
	}
	/**
	 * 添加这个方法来处理Bitmap类型参数
	 * @param v
	 * @param bitmap
	 */
	public void setViewImage(ImageView v, Bitmap bitmap) {
		v.setImageBitmap(bitmap);
    }
}

 

使用这个类替代SimpleAdapter就可以放SD卡、数据库、网络图片了(先将图片转换成Bitmap

 

 

适配器布局(res/layout/message_list.xml)

 

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent">
	<ImageView android:id="@+id/ml_icon"
		android:layout_width="50dp"
		android:layout_height="58dp"
		android:src="@drawable/ml_default_image"
		/>
	<LinearLayout
	  xmlns:android="http://schemas.android.com/apk/res/android"
	  android:orientation="vertical"
	  android:layout_width="match_parent"
	  android:layout_height="58dp"
	  android:layout_weight="1">
	  <TextView android:id="@+id/ml_title"
	  	android:layout_width="fill_parent"
	  	android:layout_height="25dp"
	  	android:textStyle="bold"
	  	android:textSize="20dp"
	  	/>
	  <TextView android:id="@+id/ml_short_content"
	  	android:layout_width="fill_parent"
	  	android:layout_height="35dp"
	  	/>
	</LinearLayout>
	<TextView
		android:layout_width="30dp"
		android:layout_height="58dp"
		android:text=">"
		android:textSize="26dp"
		android:layout_gravity="right"
		android:gravity="center"
		/>
</LinearLayout>
  • 大小: 14.3 KB
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics