У меня есть несколько маркеров на карте google и в каждом маркере ListView с несколькими записями. Каждая запись может понравиться пользователю, и если бы он понравился, в базе данных SQLite хранится запись с идентификатором маркера, идентификатором записи, и если он ему понравился (1) или взял подобное (0) и активность перезагружается. Теперь я хочу, чтобы наполненное сердце отображалось под каждым пунктом списка, который понравился пользователю. Проблема в том, что, особенно если есть много записей, есть случайно заполненные сердца, даже если пользователю понравилась только одна запись. Эти ложные заполненные сердца иногда появляются только при прокрутке вверх поэтому я предполагаю, что ListView не обновляет свои элементы при прокрутке ... Вот мой код: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 - ваш друг! – Marcus