Android Custom Cursor Adapter

走远了吗. 提交于 2019-12-24 08:23:04

问题


I have created an app for contact app. i have a problem in my cutom cursor adapter that has a two textview and Image view every time i scroll up and down the images is repeating on the other list item. anyone can help me on this one. thanks in advance.

here's a code

@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:


Here's a sample implementation of newView() and 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);
    }
}

Also, for imageUri, you might want to get it from your cursor, too...
Currently, you are using the same URI for all list items



来源:https://stackoverflow.com/questions/11242331/android-custom-cursor-adapter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!