ListView 在滚动时不改变元素

ListView does not change elements at scrolling

我在 google 地图上有几个标记,每个标记中有一个包含多个条目的 ListView。 用户可以点赞每个条目,如果他点赞,则在 SQLite 数据库中存储一个条目,其中包含标记 ID、条目 ID 以及他是否点赞 (1) 或收回点赞 (0) 以及 activity 重新加载。 现在我希望在用户喜欢的每个列表项下方显示一颗充满的心。问题是:特别是如果有很多条目,即使用户只喜欢一个条目,也会随机填充红心。这些错误填充的心有时只在向上滚动时出现,所以我假设 ListView 不会在滚动时更新其元素...... 这是我的代码:

@Override
public View getView(final int position, View convertView, ViewGroup parent) {

    final ListCell cell;
    if (convertView == null) {
        convertView = inflater.inflate(R.layout.pinboard_list_view_cell, null);
        cell = new ListCell();
        cell.likes = (TextView) convertView.findViewById(R.id.listViewLikes);
        cell.note = (TextView) convertView.findViewById(R.id.listViewNote);
        cell.img = (ImageView) convertView.findViewById(R.id.listViewImg);
        cell.likeImage = (ImageView) convertView.findViewById(R.id.heartImage);
        convertView.setTag(cell);
    }
    else {
        cell = (ListCell)convertView.getTag();
    }
    cell.position = position;

    //Listen-Items mit entsprechenden Elementen aus dem heruntergeladenen Array befüllen
    try {
        JSONObject jsonObject = this.dataArray.getJSONObject(position);

        cell.likes.setText(jsonObject.getString("likes"));
        cell.note.setText(jsonObject.getString("note"));
        cell.entryID = jsonObject.getString("id");
        String img = jsonObject.getString("image");
        String urlForImageInServer = baseUrlForImage + img;

        Picasso.with(context)
            .load(urlForImageInServer)
            .placeholder(R.drawable.progress_animation)
            .error(R.drawable.no_picture)
            .into(cell.img);


        objectID  = ""+cell.entryID;
        dbh = new DbHelper(context);
        cursor = getLikes(dbh);
        cursor.moveToFirst();
        if (cursor.moveToFirst()) {
            do {
                if (Integer.parseInt(cursor.getString(2)) == 1) {
                    cell.likeImage.setImageResource(R.drawable.heart_filled);
                }
                else {
                    cell.likeImage.setImageResource(R.drawable.heart);
                }
            }
            while(cursor.moveToNext());
        }
        else {
            cursor.close();
        }
        cursor.close();

    }
    catch (JSONException e) {
        e.printStackTrace();
    }
    return convertView;

}

public static class ListCell {
    private TextView likes;
    private TextView note;
    private ImageView img;
    public ImageView likeImage;
    public int position;
    public String entryID;
}


public Cursor getLikes(DbHelper dbh) {
    dbase = dbh.getReadableDatabase();
    String columns[] = {dbh.LIKES_MARKERID, dbh.LIKES_ENTRYID, dbh.LIKES_LIKE};
    String selection = dbh.LIKES_MARKERID + " LIKE ? AND " + dbh.LIKES_ENTRYID + " LIKE ? ";
    String args[] = {markerID.toString(), objectID};
    Cursor cursor = dbase.query(dbh.TABLE_LIKES, columns, selection, args , null, null, null, null);
    return cursor;
}

如果没有点赞确保您明确设置禁用心形图像。现在看来您正在尝试将其设置在 do while loop 内,如果流程不进入此循环内,将使用循环视图,它可能有也可能没有禁用心脏。