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[]) { //備忘1Bitmap 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中沒有實現的)。
記錄下自己的所學,雖然淺薄!!!如果我是一隻蝸牛,那就只能慢慢向上爬!