ListView不会在滚动时更改元素

时间:2015-07-27 13:04:33

标签: android sqlite listview android-listview listitem

我在谷歌地图上有几个标记,在每个标记中有一个包含多个条目的ListView。 每个条目都可以被用户喜欢,如果他喜欢,则在SQLite数据库中存储一个条目,其中包含标记ID,条目ID以及他是否喜欢(1)或取回类似(0)和活动被重新加载。 现在,我希望在用户喜欢的每个列表项下面显示一个充满希望的心。问题是:特别是如果有很多条目,即使用户只喜欢一个条目,也会随机填充。这些错误填充的心脏有时只出现在向上滚动,所以我假设,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;
}

1 个答案:

答案 0 :(得分:0)

如果没有喜欢请确保明确设置禁用心脏图像。现在看来你正试图在do while loop内设置它,如果流不进入这个循环,将使用可能会或可能没有心脏的循环视图。