问题
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