如果光标具有特定值,则在ListView中更改图像

时间:2015-07-27 11:08:29

标签: android sqlite listview android-listview

我在谷歌地图上有几个标记,在每个标记中有一个包含多个条目的ListView。每个条目都可以被用户喜欢,如果他喜欢,则在SQLite数据库中存储一个条目,其中包含标记ID,条目ID以及他是否喜欢(1)或将其取回(0)。 现在,我希望在用户喜欢的每个列表项下面显示一个充满希望的心。问题是:特别是如果有很多条目,即使用户只喜欢一个条目,也会随机填充。有人能搞清楚我的错误吗?

活动,如果保存条目,则保存在SQLite数据库中:

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();
                }
            }

        }
    });

适配器,设置listView的元素,如果喜欢该条目,则将imgage更改为heart_filled:

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

也许我应该补充说SQLite条目似乎是正确添加的,因为如果我喜欢错误填充的一个条目,它不会填充它,但会保持填充并增加喜欢的数量。只有这样,我才能不喜欢&#34;它然后再次没有填充心脏。

1 个答案:

答案 0 :(得分:0)

第一个想法:如果您只有1表示喜欢而0表示不喜欢,请将您的查询更改为if / else而不是if / else if;这个可能的错误来源更少。

第二个想法更新:关于我在帖子中发布评论的内容,但是对于这些事情没有足够的经验来做出明确的陈述。