1

我为联系人应用程序创建了一个应用程序。我的自定义光标适配器有一个问题,每次我上下滚动时都有两个文本视图和图像视图,图像在另一个列表项上重复。任何人都可以帮助我解决这个问题。提前致谢。

这是一个代码

@Override
public void bindView(View view, Context context, Cursor cursor) {
    if(view !=null){

        ViewHolder holder = initViewHolder(view);

        holder.displayName.setText(getUserDisplayName(cursor.getString(userid)));
        holder.groupId.setText(cursor.getString(group));

                holder.displayPhoto.setImageURI(imageUri);

    }

}

private ViewHolder initViewHolder(View view) {
    ViewHolder innerViewHolder = null;
    if(innerViewHolder == null){
        innerViewHolder = new ViewHolder();
        innerViewHolder.displayName  =(TextView) view.findViewById(R.id.name);
        innerViewHolder.groupId = (TextView) view.findViewById(R.id.groupId);
        innerViewHolder.displayPhoto = (ImageView) view.findViewById(R.id.photo);
    }
    return innerViewHolder;
}


class ViewHolder{
    TextView displayName;
    TextView groupId;
    ImageView displayPhoto;
}

@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
    return inflater.inflate(R.layout.contact_list_item, null);
}
4

1 回答 1

2

这是 newView() 和 bindView() 的示例实现。

public View newView(Context context, Cursor cursor, ViewGroup parent) {
    View view = inflater.inflate(R.layout.list_item_whatever, null);
    ViewHolder holder = new ViewHolder();
    holder.displayName = (TextView) view.findViewById(R.id.name);
    holder.groupId = (TextView) view.findViewById(R.id.groupId);
    holder.displayPhoto = (ImageView) view.findViewById(R.id.photo);
    view.setTag(holder);
    return view;
}

@Override
public void bindView(View view, Context context, Cursor cursor) {
    ViewHolder holder = (ViewHolder) view.getTag();
    if (holder.displayName != null) {
        holder.displayName.setText(getUserDisplayName(cursor.getString(userid)));
    }
    if (holder.groupId != null) {
        holder.groupId.setText(cursor.getString(group));
    }
    if (holder.displayPhoto != null) {
        holder.displayPhoto.setImageURI(imageUri);
    }
}

此外,对于 imageUri,您可能也希望从光标中获取它...
目前,您对所有列表项使用相同的 URI

于 2012-06-28T10:40:40.340 回答