SimpleAdapter的理解与简单实现

  • 首先SimpleAdapter接口继承于Adapter接口。Adapter是Android里的适配器,充当数据和界面之间的桥梁,相当于一个数据源,可以给适用的AdapterView提供数据,也就是说可以将各类型的数据转换成符合界面风格形式的数据,然后可以通过AdapterView显示出来,SimpleAdaper的作用是方便地将数据与XML文件定义的各种View绑定起来,从而创建复杂的UI。
    值得提一提的是SimpleAdapter并不像字面上的意思那样简单,SimpleAdapter的功能不仅不简单,还十分强大,列表组件的大部分使用都是通过SimpleAdapter来提供列表项的。

    • 我们先看看SimpleAdapter的构造函数:
    public SimpleAdapter(Context context, List<? extends Map<String, ?>> data,
            @LayoutRes int resource, String[] from, @IdRes int[] to) {
        mData = data;
        mResource = mDropDownResource = resource;
        mFrom = from;
        mTo = to;
        mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }
    
    

    可以看到其中五个参数分别代表着
    context:要使用的上下文环境,大多数情况下都是填this。

    data:是一个List<? extends Map<String,?>>类型的键值对,该集合中每个Map<String, ?>对象生成一个列表项。

    resource:界面布局文件的ID,对应的布局文件作为列表项的组件。

    from:是一个String[]类型的参数,该参数也就是data中的Map<String, ?>对象中哪些key对应的value来生成列表项。

    to:该参数是一个int[]类型的参数,该参数决定填充哪些组件。

    • SimpleAdapter的源码l链接
    • Adapter的源码链接
      看过源码之后我们可以看到SimpleAdapter继承了Adapter之后实现了覆写了getCount、getItem、getItemId、getView和getDropDownView,其中getView和getDropDownView都调用了createViewFromResource方法,下面是createViewFromResource的源码:
    private View createViewFromResource(LayoutInflater inflater, int position, View convertView,
            ViewGroup parent, int resource) {
        View v;
        if (convertView == null) {
            v = inflater.inflate(resource, parent, false);
        } else {
            v = convertView;
        }
    
        bindView(position, v);
    
        return v;
    }
    

createViewFromResource方法是SimpleAdapter接口新增的方法,在createViewFromResource中,会调用bindView方法,bindView方法的作用就是将数据项与对应的View绑定起来,从而使得View在界面上展现出数据内容。
bindView方法如下:

  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;
		//依次遍历在getCount所得的所有视图,通过findViewById()方法找到相应的视图文件
        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;
               //binder对象中的setView可以将View和data绑定在一起显示在View中,如果非空即绑定在一起
                if (binder != null) {
                    bound = binder.setViewValue(v, data, text);
                }
		//这里是如果binder不存在或没绑定成功时,SimpleAdapter会自定将数据绑定在一起
                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 {
                            setViewImage((ImageView) v, text);
                        }
                    } else {
                        throw new IllegalStateException(v.getClass().getName() + " is not a " +
                                " view that can be bounds by this SimpleAdapter");
                    }
                }
            }
        }
    }

  1. 首先通过ViewBinder实现开发者自己绑定数据
    SimpleAdapter中内部有一个ViewBinder类型的成员变量mViewBinder,通过SipmleAdater的setViewBinder方法可以对其赋值,mViewBinder的默认值是null。
    ViewBinder是SimpleAdapter的一个内部接口,其定义了setViewValue方法。我们可以定义一个对象,实现ViewBinder接口的setViewValue方法,然后通过setViewBinder赋值给mViewBinder。
    在bindView方法中,会首先判断mViewBinder存不存在,如果存在就调用mViewBinder的setViewValue方法,该方法会返回一个boolean值,如果返回true表示开发者自己已经成功将数据和View绑定起来了,
    bound值为true,后面就不会再执行其他逻辑。

  2. 如果开发者没有自己绑定数据(这是常见的情形),那么SimpleAdapter会自己尝试去绑定数据。具体来说,如果mViewBinder不存在或者mViewBinder的setViewValue方法返回false,那么bound值为false,这时候Android就 会按照自己的逻辑尽量去将数据和View进行绑定。

SimpleAdapter代码实例

以listview和SimpleAdapter相结合的方式实现一个简单的UI界面:

SimpleAdapter.java

private String[] names = new String[]{"Lion", "Tiger", "Monkey", "Dog", "Cat", "elephant"};
    private int[] image = new int[]{R.drawable.lion, R.drawable.tiger, R.drawable.monkey, R.drawable.dog, R.drawable.cat, R.drawable.elephant};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        //此处引用布局文件
        setContentView(R.layout.activity_simple_adapter);
        final int color1 = 0xFFC5B5FF;
        final int color2 = 0xFFFFFFFF;
        //创建一个list集合,list集合的元素是Map
        List<Map<String, Object>> ListItems = new ArrayList<Map<String, Object>>();
        for (int i = 0; i < names.length; i++) {
            Map<String, Object> listItem = new HashMap<String, Object>();
            listItem.put("header", names[i]);
            listItem.put("images", image[i]);
            //加入list集合
            ListItems.add(listItem);
        }
        //创建一个SimpleAdapter,此处严格按照定义数组names与image顺序,否则会出现程序build成功却运行失败且难以解决错误
        SimpleAdapter simpleAdapter = new SimpleAdapter(this, ListItems, R.layout.items, new String[]{"header", "images"}, new int[]{R.id.header, R.id.images});
        final ListView list = (ListView) findViewById(R.id.listview);
        //为ListView设置Adapter
        list.setAdapter(simpleAdapter);

activity_simple_adapter.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="wrap_content"
    android:orientation="horizontal">
    <ListView
        android:id="@+id/listview"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:listSelector="@color/colorAccent"
        >
    </ListView>

</LinearLayout>

展示效果

 SimpleAdapter

作者:王帅
原文链接
参考:SimpleAdapter源码详解

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值