lundi 27 juillet 2015

ListView does not change elements at scrolling

I have several markers on a google map and in each marker a ListView with several entries. Each entry can be liked by the user and if he has liked, there is stored an entry in the SQLite Database with the marker ID, the entry ID and if he has liked (1) or took the like back (0) and the activity is reloaded. Now I want a filled heart to be shown below each List Item the user has liked. The problem is: Especially if there are many entries, there are randomly filled hearts, even if the user only liked one entry. These falsely filled hearts sometimes appear only at scrolling up so I assume, that the ListView does not update its elements at scrolling... Here is my code:

@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;
}

Aucun commentaire:

Enregistrer un commentaire