SimpleAdapter的用法

学习listView的时候,按照例子设定item的布局为系统提供的simple_list_item_single_choice.xml@frameworks/base/core/res/res/layout/ 加上SimpleAdapter,感觉很爽,什么都不用写直接就用了,然后我就自己定义了一个布局一个ImageView和一个CheckedTextView,问题来了,点击不选中,但是使用SimpleAdapter的时候可是好好的。我决定肯定是SimpleAdapter做了什么事情可以很好的实现单选(后来发现与simpleadapter完全无关,只是CheckedTextView实现了选择功能)。于我决定认真的研究一下SimpleAdapter。

SimpleAdapter继承于BaseAdapter,代码也不多@frameworks/base/core/java/android/widget/SimpleAdapter.java

最最重要的一个函数是bindView,它被getView调用。它个是它使用拿数据适配组件的关键

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 {
                            setViewImage((ImageView) v, text);
                        }
                    } else {
                        throw new IllegalStateException(v.getClass().getName() + " is not a " +
                                " view that can be bounds by this SimpleAdapter");
                    }
               }
            }
        }
    }

注意我做成红色标记的这四个点,暂时不管第四个,先说说前三个。

在此之前先看一个SimpleAdapter的用法

new SimpleAdapter(getActivity(),
                buildData(),R.layout.logo,
                new String[]{"img","text","check"},
                new int[]{R.id.logo, R.id.tvListItem,R.id.tvListItem});

红色标记①是给实现Checkable接口的TextView赋值,所以它检查值的类型,如果是boolean它就明白是设置成选中状态,如果是TextView就是赋字符值。所以要给CheckedTextView赋值,需要写两次就像我上面的例子一样text和check一个是显示的内容,一个是批选中状态,所以下面我写了两次R.id.tvListItem。

除些之后它还可以给ImageView赋值,所就是说只要你的控件是由CheckedTextView,TextView和ImageView中的一种或者几种组合而成,不管个数是多少个,都可以用SimpleAdapter做数据适配。

再看④,这就更牛B了,先看一下ViewBinder的定义

public static interface ViewBinder {
        boolean setViewValue(View view, Object data, String textRepresentation);
    }

在不用自己实现Adapter的情况下,自定义数据的适配,simpleAdapter的适用范围又扩大了很多,简直是万能的了。自定义一个控件,然后自己实现viewBinder接口,设置到SimpleAdapter中,就可以用它来适配你自己的控件了。

posted @ 2015-01-18 21:02  _Gelandesprung  阅读(2579)  评论(0编辑  收藏  举报