【发布时间】:2015-10-17 13:05:29
【问题描述】:
我在谷歌地图上有几个标记,每个标记都有一个包含多个条目的 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的元素,如果条目被点赞,则将图像更改为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 条目似乎添加正确,因为如果我喜欢一个被错误填充的条目,它不会取消填充它,而是保持填充并增加喜欢的数量。只有这样我才能“不喜欢”它,然后心又是空虚的。
【问题讨论】:
-
也许是因为我没有经常使用 sql 和 picasso,但是在你的 'do{if/else if}while(cursor.moveToNext());' 中,我看不到你的单元格发生了变化,所以它似乎会为数据库中的每一个类似的单元格更改相同的单元格?
标签: android sqlite listview android-listview