setViewBinder/setViewValue 和 getView/LayoutInflater 的用法有什么区别?

发布于 2024-11-14 01:54:30 字数 1001 浏览 3 评论 0原文

看起来有两种可能的方法可以更改 ListView 行中的某些内容:

  1. 使用 setViewBinder/setViewValue

    myCursor.setViewBinder(new SimpleCursorAdapter.ViewBinder() {

    <预><代码> @Override public boolean setViewValue(View view, Cursor 光标, int columnIndex) { int viewId = view.getId(); 开关(视图ID){ 案例 R.id. 图标: // 在这里更改与图标相关的内容
  2. 使用getView/<代码>LayoutInflater:

    公共视图 getView(int 位置,视图 ConvertView,ViewGroup 父级){

     查看 itemView = null;
    
        如果(convertView == null){
            LayoutInflater 充气器 = (LayoutInflater)parent.getContext()
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            itemView = inflater.inflate(R.layout.list_row, null);
        } 别的 {
            itemView = 转换视图;
        }
    
        ImageView imgViewChecked = (ImageView) itemView
                .findViewById(R.id.icon);
        // 在这里更改与图标相关的内容
    

这两种方法有什么区别?

Looks like there are two possible ways to change something in the ListView rows:

  1. using of setViewBinder/setViewValue:

    myCursor.setViewBinder(new SimpleCursorAdapter.ViewBinder() {

      @Override
      public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
        int viewId = view.getId();
        switch(viewId) {
        case R.id.icon:
            // change something related to the icon here
    
  2. using of getView/LayoutInflater:

    public View getView(int position, View convertView, ViewGroup parent) {

        View itemView = null;
    
        if (convertView == null) {
            LayoutInflater inflater = (LayoutInflater) parent.getContext()
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            itemView = inflater.inflate(R.layout.list_row, null);
        } else {
            itemView = convertView;
        }
    
        ImageView imgViewChecked = (ImageView) itemView
                .findViewById(R.id.icon);
        // change something related to the icon here
    

What is the difference between these two approaches?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

日久见人心 2024-11-21 01:54:30

您可以使用它们来完成相同的任务。
ViewBinder 系统是由 SimpleCursorAdapter 添加的,使您的事情变得更容易,因此您不必编写整个 getView 代码。事实上,SimpleCursorAdapter 只是通过调用 setViewValue 方法(以及标准样板错误检查和膨胀)来实现 getView

我已经附上了 Android 源代码在 SimpleCursorAdapter 中用于 getView 的实现:

public View getView(int position, View convertView, ViewGroup parent) {
  if (!mDataValid) {
    throw new IllegalStateException(
        "this should only be called when the cursor is valid");
  }
  if (!mCursor.moveToPosition(position)) {
    throw new IllegalStateException("couldn't move cursor to position "
        + position);
  }
  View v;
  if (convertView == null) {
    v = newView(mContext, mCursor, parent);
  } else {
    v = convertView;
  }
  bindView(v, mContext, mCursor);
  return v;
}


public void bindView(View view, Context context, Cursor cursor) {
  final ViewBinder binder = mViewBinder;
  final int count = mTo.length;
  final int[] from = mFrom;
  final int[] to = mTo;

  for (int i = 0; i < count; i++) {
    final View v = view.findViewById(to[i]);
    if (v != null) {
      boolean bound = false;
      if (binder != null) {
        bound = binder.setViewValue(v, cursor, from[i]);
      }

      if (!bound) {
        String text = cursor.getString(from[i]);
        if (text == null) {
          text = "";
        }

        if (v instanceof TextView) {
          setViewText((TextView) v, text);
        } else if (v instanceof ImageView) {
          setViewImage((ImageView) v, text);
        } else {
          throw new IllegalStateException(
              v.getClass().getName()
                  + " is not a "
                  + " view that can be bounds by this SimpleCursorAdapter");
        }
      }
    }
  }
}

You can use both of them to accomplish the same task.
The ViewBinder system is added by SimpleCursorAdapter to make things easier for you, so you don't have to write the entire getView code. In fact, SimpleCursorAdapter just implements getView by calling the setViewValue method (along with the standard boilerplate error checking and inflating)

I've attached the implementation that the Android source code uses for getView in SimpleCursorAdapter:

public View getView(int position, View convertView, ViewGroup parent) {
  if (!mDataValid) {
    throw new IllegalStateException(
        "this should only be called when the cursor is valid");
  }
  if (!mCursor.moveToPosition(position)) {
    throw new IllegalStateException("couldn't move cursor to position "
        + position);
  }
  View v;
  if (convertView == null) {
    v = newView(mContext, mCursor, parent);
  } else {
    v = convertView;
  }
  bindView(v, mContext, mCursor);
  return v;
}


public void bindView(View view, Context context, Cursor cursor) {
  final ViewBinder binder = mViewBinder;
  final int count = mTo.length;
  final int[] from = mFrom;
  final int[] to = mTo;

  for (int i = 0; i < count; i++) {
    final View v = view.findViewById(to[i]);
    if (v != null) {
      boolean bound = false;
      if (binder != null) {
        bound = binder.setViewValue(v, cursor, from[i]);
      }

      if (!bound) {
        String text = cursor.getString(from[i]);
        if (text == null) {
          text = "";
        }

        if (v instanceof TextView) {
          setViewText((TextView) v, text);
        } else if (v instanceof ImageView) {
          setViewImage((ImageView) v, text);
        } else {
          throw new IllegalStateException(
              v.getClass().getName()
                  + " is not a "
                  + " view that can be bounds by this SimpleCursorAdapter");
        }
      }
    }
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文