В чем разница между использованием setViewBinder / setViewValue и getView / LayoutInflater? - PullRequest
3 голосов
/ 06 июня 2011

Похоже, есть два возможных способа что-то изменить в строках ListView:

  1. с использованием 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. с использованием getView / LayoutInflater:

    public View getView (int position, View convertView,Родитель ViewGroup) {

        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
    

В чем разница между этими двумя подходами?

1 Ответ

3 голосов
/ 06 июня 2011

Вы можете использовать их обоих для выполнения одной и той же задачи.SimpleCursorAdapter добавляет систему ViewBinder, чтобы упростить вам задачу, поэтому вам не нужно писать весь код getView.Фактически, SimpleCursorAdapter просто реализует getView, вызывая метод setViewValue (вместе со стандартной проверкой и раздуванием стандартных шаблонов ошибок)

Я добавил реализацию, которую исходный код Android использует для getView в 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");
        }
      }
    }
  }
}
...