2

对于我的 android 应用程序,我正在使用 ViewHolder 模式。每当我单击列表视图中的图像视图时,该行都会更新,但其他行也会更新。

每当我单击“喜欢”按钮时,其他图像也会收到用户喜欢的图像。这怎么可能?

这是我的查看器代码:

public class MyAdapter extends ArrayAdapter<MyModel> {

Context context;
int layoutResourceId;
List<MyModel> data = null;

public MyAdapter(Context context, int layoutResourceId,
        List<MyModel> data) {
    super(context, layoutResourceId, data);
    this.layoutResourceId = layoutResourceId;
    this.context = context;
    this.data = data;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View row = convertView;
    final BlockHolder holder;

    if (row == null) {
        LayoutInflater inflater = ((Activity) context).getLayoutInflater();
        row = inflater.inflate(layoutResourceId, parent, false);
        holder = new BlockHolder();

        holder.imgIcon = (ImageView) row.findViewById(R.id.imgIcon);
        holder.totalVotes = (TextView) row.findViewById(R.id.totalVotes);
        holder.likeIcon = (ImageView) row.findViewById(R.id.likeIcon);
        holder.dislikeIcon = (ImageView) row.findViewById(R.id.dislikeIcon);
        holder.saveIcon = (ImageView) row.findViewById(R.id.saveIcon);

        row.setTag(holder);
    } else {
        holder = (BlockHolder) row.getTag();
    }

    MyModel myBlock = data.get(position);

    ImageLoader.getInstance().displayImage(myBlock.getImage(),
            holder.imgIcon);

    final int totalLikes = myBlock.getLikes() - myBlock.getDislikes();

    holder.totalVotes.setText(totalLikes + " likes");
    final ImageView like = holder.likeIcon;

    holder.likeIcon.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            // ALSO UPDATES OTHER VIEWS...
            holder.likeIcon.setBackgroundResource(R.drawable.thumbs_up_mouse);
        }

    });

    return row;
}

private static class BlockHolder{
    ImageView imgIcon;
    TextView totalVotes;
    ImageView dislikeIcon;
    ImageView likeIcon;
    ImageView saveIcon;
}
4

0 回答 0