3

On dirait qu'il ya deux façons de changer quelque chose dans les ListView lignes:Quelle est la différence entre l'utilisation de setViewBinder/setViewValue et getView/LayoutInflater?

  1. utilisant des setViewBinder/setViewValue:

    myCursor.setViewBinder (nouveau 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. en utilisant getView/LayoutInflater:

    publique Voir getView (int position, Vue convertView, parent 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 
    

Quelle est la différence entre ces deux approches?

Répondre

3

Vous pouvez utiliser les deux pour accomplir la même tâche. Le système ViewBinder est ajouté par SimpleCursorAdapter pour vous simplifier la tâche. Vous n'avez donc pas à écrire le code entier de getView. En fait, SimpleCursorAdapter met en œuvre tout getView en appelant la méthode de setViewValue (ainsi que la vérification standard erreur de texte standard et le gonflage)

J'ai joint la mise en œuvre que le code source Android utilise pour getView dans 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"); 
     } 
     } 
    } 
    } 
} 
Questions connexes