Android自定义光标适配器

时间:2012-06-28 10:08:32

标签: android android-cursoradapter

我为联系人应用创建了一个应用。我有一个问题在我的cutom光标适配器有两个textview和图像视图每次我上下滚动图像重复在另一个列表项目。任何人都可以帮助我。提前致谢。

这是一个代码

@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);
}

1 个答案:

答案 0 :(得分: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