android ListView中自定义SimpleAdapter动态添加ratingBar及图片更新
android中listView的实现有多种方式,但是当我们利用simpleAdapter实现listView的时候,SimpleAdapter并没有提供显示一个ratingBar的功能(即上面的星星用来评分的控件)。要实现上面的功能,我们可以通过继承SimpleAdapter类重写其中一些方法来实现。下面即是此类的实现:
package nate.android.Service; import java.util.List; import java.util.Map; import com.nate.wte.LocalSql.StoresInfoDB; 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.RatingBar; import android.widget.SimpleAdapter; import android.widget.TextView; public class MySimpleAdapter 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 MySimpleAdapter(Context context, List<? extends Map<String, ?>> data, int resource, String[] from, int[] to) { super(context, data, resource, from, to); mData = data; mResource = mDropDownResource = resource; mFrom = from; mTo = to; mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); } /** * @see android.widget.Adapter#getView(int, View, ViewGroup) */ public View getView(int position, View convertView, ViewGroup parent) { return createViewFromResource(position, convertView, parent, mResource); } private View createViewFromResource(int position, View convertView, ViewGroup parent, int resource) { View v; if (convertView == null) { v = mInflater.inflate(resource, parent, false); final int[] to = mTo; final int count = to.length; final View[] holder = new View[count]; for (int i = 0; i < count; i++) { holder[i] = v.findViewById(to[i]); } v.setTag(holder); } 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 View[] holder = (View[]) view.getTag(); final String[] from = mFrom; final int[] to = mTo; final int count = to.length; for (int i = 0; i < count; i++) { final View v = holder[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 { throw new IllegalStateException(v.getClass().getName() + " should be bound to a Boolean, not a " + 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); ((TextView) v).setText(text); } else if (v instanceof ImageView) { if (data instanceof Integer) { setViewImage((ImageView) v, (Integer) data); } else if(data instanceof byte[]) { //备注1 Bitmap bmp; byte[] image = (byte[])data; if(b.length!=0){ bmp = BitmapFactory.decodeByteArray(b, 0, b.length); ((ImageView) v).setImageBitmap(bmp); } } else if(v instanceof RatingBar){ float score = Float.parseFloat(data.toString()); //备注2 ((RatingBar)v).setRating(score); } else { throw new IllegalStateException(v.getClass().getName() + " is not a " + " view that can be bounds by this SimpleAdapter"); } } } } } public void setViewImage(ImageView v, int value) { v.setImageResource(value); }
以上MySimpleAdapter类继承自SimpleAdapter,我们要实现的效果主要在 private void bindView(int position, View view) 函数中添加(更改)就行。这个函数中相当于将控件与数据对应,然后在应用此类时,如下
MySimpleAdapter myListAdapter = new MySimpleAdapter(this,list,R.layout.stores, new String[]{"store_name", "store_score","store_pic","store_scores","phone"}, new int[]{R.id.store_name, R.id.store_score,R.id.store_pic,R.id.store_score_bar,R.id.showPhone});此adapter内容与上面图中显示的listView中的item内容一致。
myListView.setAdapter(myListAdapter);
在备注2处实现了当View是一个RatingBar的类型时也能在listView中显示的效果的功能。此时我们的data就是我们要显示的ratingBar中star的个数(也就是评分数) !!!
在备注1处,我们将网络或者是从数据库等地方取来的图片数据(Blob类型然后转成byte[]类型)(比如我们之前可以使用
public byte[] Bitmap2Bytes(Bitmap bm){ ByteArrayOutputStream baos = new ByteArrayOutputStream(); bm.compress(Bitmap.CompressFormat.PNG, 100, baos); return baos.toByteArray(); }将图片转成byte[]类型存储)。扯远了!这个转换在这篇文章中有将详细的说明。
总之备注一处就是运用上面添加ratingBar同样的原理将byte[]类型的图片还原(这是原来的SimpleAdapter中没有实现的)。
记录下自己的所学,虽然浅薄!!!如果我是一只蜗牛,那就只能慢慢向上爬!