Android的ListView中添加数据库中的Image和RatingBar

来源:互联网 发布:js页面关闭时触发事件 编辑:程序博客网 时间:2024/05/22 06:28

最近和同学在一起讨论做一个小项目,不过今天晚上就是指定期限了,我们还是没有做出一个看得过去的东西出来,有点小失望,不过没关系,这也只是一个开始,毕竟学Android的时间还这么短,还是有上升空间的。

这段时间遇到了一些问题,自己的能力方面也得到了些许提高,接下来对这段时间做一个总结:

1. Android的ListView中添加数据库中的Image和RatingBar

首先说一下RatingBar:

想要调取数据库中的数据动态的显示RatingBar,需要复写SimpleAdapter

下面贴出大牛的代码,望能给大家提供一些思路:

package com.ezz.bytourism1;import java.io.File;import java.util.List;import java.util.Map;import android.content.Context;import android.graphics.Bitmap;import android.graphics.BitmapFactory;import android.util.Log;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;import android.widget.Toast;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 RatingBar){                    float score = Float.parseFloat(data.toString());  //备注2                    //Log.i("ratingBar", score+" ");                    ((RatingBar)v).setRating(score);                    }else  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 String){//File file = new File(data.toString());File file = new File("E://image/xiamen_three.jpg");                    if(file.exists()){Bitmap bm = BitmapFactory.decodeFile("/image/xiamen_three.jpg");((ImageView)v).setImageBitmap(bm);}else{file = new File("E://image//xiamen_three.jpg");Log.i("file.exists = ", ""+file.exists());}                    }*/                    if (data instanceof Integer) {                            setViewImage((ImageView) v, (Integer) data);                                                    }                     else if(data instanceof byte[]) {      //备注1Bitmap bmp;byte[] image = (byte[])data;if(image.length!=0){bmp = BitmapFactory.decodeByteArray(image, 0, image.length);((ImageView) v).setImageBitmap(bmp);}}                        }                                        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);    }}

主要看的代码块是:bindView(int position,View view),holder里面获得了适配器中所要定义的控件的View,data表示要给控件附上的值,v instanceof RatingBar,表示的是当前的View是RatingBar类型的,如果是RatingBar类型的,则取出data中的数据,并将它们转化为一个float类型的量,最后设置在RatingBar上。

接下来说一下将数据库中的Image提取出来的问题:

首先你要知道你数据库中存放的Image是什么类型,上面代码的备注1,分别表示了,图像以二进制数据保存在数据库中和图像以R中id的形式保存在数据库中的两种情况。

然而如果你在数据库中存放的是Image的路径,则需要使用到ImageLoader,因为如果你使用的是服务器上的路径,这在手机中是找不到的,所以不能直接使用数据库中的路径来找对应图片,如果你是存放在SD卡中,你可以修改一个File中的Path,把路径设置到SD卡中,记得这时候你要在Manifest.xml文件里面添加SD卡的读取权限。这里我不细说,因为我也还没做到这里呢~~我打算继续熟悉一些基础,得到对Android轮廓熟悉以后在通过读源码来加强自己对Android的理解。

0 0