【发布时间】:2015-10-17 16:27:27
【问题描述】:
我在谷歌地图上有几个标记,每个标记中有一个 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;
}
【问题讨论】:
-
这种行为是正常的。这个问题已经被问过好几次了。 Google 是您的朋友!
标签: android sqlite listview android-listview listitem