lundi 27 juillet 2015

Change Image in ListView if cursor has certain value

I have several markers on google maps 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). 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. Can someone figure out my mistake?

Activity, that saves in the SQLite Database if entry is saved:

getALlEntrysListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                try {
                    JSONObject entryClicked = jsonArray.getJSONObject(position);
                    entryID = entryClicked.getString("id");
                } catch (JSONException e) {
                    e.printStackTrace();
                }

                likeButton = (ImageView) view.findViewById(R.id.heartImage);
                pos = "" + position;
                int objectID = Integer.parseInt(entryID);
                Cursor cursor = getLikes(dbh);
                cursor.moveToFirst();

                if (cursor.moveToFirst()) {
                    do {
                        if (Integer.parseInt(cursor.getString(2)) == 1) {
                            dbh.updateLike(dbh, markerID, objectID, Integer.parseInt(cursor.getString(2)), 0);
                            dislike(entryID);
                            cursor.close();
                        } else if (Integer.parseInt(cursor.getString(2)) == 0) {
                            dbh.updateLike(dbh, markerID, objectID, Integer.parseInt(cursor.getString(2)), 1);
                            likeUpload(entryID);
                            cursor.close();
                        } else {
                            dbh.addLike(dbh, markerID, objectID, 1);
                            likeUpload(entryID);
                            cursor.close();
                        }
                    } while (cursor.moveToNext());
                } else {
                    dbh.addLike(dbh, markerID, objectID, 1);
                    likeUpload(entryID);
                    cursor.close();
                }
            }

        }
    });

Adapter, who sets the elements of the listView and changes the imgage to heart_filled if the entry is liked:

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


    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 if (Integer.parseInt(cursor.getString(2)) == 0) {
                    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