Exemplo n.º 1
0
  // 学习学习再学习,学习定义自己的adapter,特定位置的特定视图,只能是图片或者文本
  // 为特定的view添加内容
  // 特定位置对view进行数据填充
  private void bindView(int position, View view) {
    final Map dataSet = mData.get(position);
    if (dataSet == null) {
      return;
    }

    final ViewBinder binder = mViewBinder;
    // 一个map中的数据,其key为string类型
    final String[] from = mFrom;
    // 对应的需要赋值的view
    final int[] to = mTo;
    // 需要进行填充的view的数量,to啊
    final int count = to.length;
    // 循环为每一个特定视图赋值
    for (int i = 0; i < count; i++) {
      final View v = view.findViewById(to[i]);
      if (v != null) {
        // 获取对应位置map对应的key的值
        final Object data = dataSet.get(from[i]);
        String text = data == null ? "" : data.toString();
        if (text == null) {
          // 防止错误
          text = "";
        }

        boolean bound = false;
        if (binder != null) {
          // bound是做什么用的呢
          bound = binder.setViewValue(v, data, text);
        }
        // 原来还可以有checkable
        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");
          }
        }
      }
    }
  }