1

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);
}
oczdref
  • 534
  • 1
  • 6
  • 15
  • post the code of your Custom Cursor Adapter. – Vit Khudenko Jun 28 '12 at 10:15
  • 1
    Refer this question http://stackoverflow.com/questions/5183813/android-issue-with-newview-and-bindview-in-custom-simplecursoradapter – Nirali Jun 28 '12 at 10:24
  • This has nothing to do with your problem, but writing `ViewHolder innerViewHolder = null;` and then `if(innerViewHolder == null)` is redundant – Bojan Radivojevic Jun 28 '12 at 10:32
  • 1
    It doesn't look like `imageUri` is being changed for individual views, might want to check that you aren't just loading the same uri for each row. – Alex Curran Jun 28 '12 at 10:36
  • is this something on the mapping of the image. im mapping the image directly to the sdcard? – oczdref Jun 29 '12 at 03:49

1 Answers1

2

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

Bojan Radivojevic
  • 717
  • 3
  • 12
  • 23
  • thanks all i resolve the problem. its a simple solution. you must set the empty ImageView to any default images. so that every time you scroll up and down the other image did not repeat on the other row. i think it is because the image it cache the image on the ui part so that it repeats on the row if (holder.displayPhoto != null) { holder.displayPhoto.setImageURI(imageUri); }else{holder.displayPhoto.setImageURI();} – oczdref Jun 29 '12 at 06:53
  • True, that is because the ListView reuses its views to optimize performance – Bojan Radivojevic Jun 29 '12 at 10:06
  • so it must not set a null or empty value on ImageView. thanks – oczdref Jun 29 '12 at 10:41