似曾相识,在viewholder 滥竽开花时,我很不想说,大多数人没学到软件工程的精髓。或者没有行之有效的将其自觉利用。

很多年前,我做了以下几件事

一. 省略了viewholder步骤

二. adapter系列用了配置的思想,配置的方式就是simpleadapter的发展

三. 接纳了Map,Object,Cursor至少三种有效的数据源。并在实现上允许自由切换

四. 将这个adapter 留下实现入口,做各种二次功能扩展,即便是一个匿名实现也可以用简单添加的方式完成特殊的自定义功能。

基本的代码如下:

if (item == null) {
            return;
        }
        if (item instanceof Cursor) { 处理//Cursor
            treatCursor(item, convertView, position);
        } else if (item instanceof Map) { //处理Map
            treatMap(item, convertView, position);
        }
        // else if (item instanceof Entity) {  自定义具体类对象,这里是实现的是对象体里包含Map的一种折衷,可以视为综合Map和Object的快捷方式 
        // treatMap(((Entity) item).fieldContents, convertView, position);
        // }
        else if (item instanceof JSONObject) { //可以处理json
            treatJSONArray(item, convertView, position);
        } else { //处理反射对象
            treatObject(item, convertView, position);
        }
     public void treatObject(Object item, View convertView, int position)
            throws SecurityException {
        Object value = null;
        String name;
        boolean isAccessible;
        clazz = item.getClass();
        Field[] fields = clazz.getFields();
        for (Field field : fields) {
            isAccessible = field.isAccessible();
            field.setAccessible(true);
            if (this.fieldnames.contains(field.getName())) { //fieldnames 送进来的配置的字段的名称
                name = field.getName();
                try {
                    value = field.get(item);
                    // value = value == null ? "" : value;
                } catch (IllegalArgumentException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IllegalAccessException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                findAndBindView(convertView, position, item, name, value);
            }
            field.setAccessible(isAccessible);
        }
    }
    public void treatCursor(Object item, View convertView, int position) {
        Object value = null;
        String name;
        Cursor cur = (Cursor) item;
        Class type;
        if (cur.getColumnCount() > 0) {
            while (cur.moveToNext()) {
                for (int i = 0; i < cur.getColumnCount(); i++) {
                    name = cur.getColumnName(i);
                    type = itemDataSrc.getNameTypePair().get(name);
                    if (type == null) {
                        continue;
                    }
                    try {
                        if (type == Integer.class) {
                            value = cur.getInt(i);
                        } else if (type == Long.class) {
                            value = cur.getLong(i);
                        } else if (type == Double.class) {
                            value = cur.getDouble(i);
                        } else if (type == String.class) {
                            value = cur.getString(i);
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    findAndBindView(convertView, position, item, name, value);
                }
            }
        }
    }

     public void treatMap(Object item, View convertView, int position) {
        String name;
        Object value;
        Map<String, Object> items = (Map<String, Object>) item;
        for (int i = 0; i < this.fieldnames.size(); i++) {
            name = this.fieldnames.get(i);
            if (items.containsKey(name)) {
                value = items.get(name);
                findAndBindView(convertView, position, item, name, value);
            }
        }
    }
    
    protected boolean findAndBindView(View convertView, int pos, Object item,
                                      String name, Object value) {
        int viewidIdx = this.fieldnames.indexOf(name);      //配置里的字段名
        View theView = convertView.findViewById(this.viewids.get(viewidIdx));  //配置里的字段id
        return setView(pos, item, value, convertView, theView); //做具体的工作
    }
    protected List<String> fieldnames;
    protected List<Integer> viewids;
 protected boolean setView(int pos, Object item, Object value,
                              View convertView, View theView) {
        if (theView == null) {
            return false;
        }
if (value == null) {
            return false;
        }
        theView.setVisibility(View.VISIBLE);
        StyleBox styleBox = null;
        if (theView instanceof ImageView) {
            if (value instanceof String&&value.toString().startsWith("http")) {
                //do imageloader ... etc
            }else if (value instanceof Integer) {
                ((ImageView) theView).setImageResource(Integer.parseInt(value
                        .toString()));
            } else if (value.getClass() == BitmapDrawable.class) {
                ((ImageView) theView).setImageDrawable((BitmapDrawable) value);
            } else if (value instanceof Drawable) {
                ((ImageView) theView).setImageDrawable((Drawable) value);
            }
        } else if (theView instanceof CheckBox) {
            ((CheckBox) theView).setChecked(Boolean.parseBoolean(value
                        .toString()));
        } else if (theView instanceof TextView) {
            ((TextView) theView)
                        .setText(value instanceof SpannableStringBuilder ?(SpannableStringBuilder) value
                                : value.toString());
        }else{
            extendView(theView,value);  //自定义某些控件
        }
        return true;
    }

05-02 10:05