android - Change Image in ListView if cursor has certain value -
i have several markers on google maps , in each marker listview several entries. each entry can liked user , if has liked, there stored entry in sqlite database marker id, entry id , if has liked (1) or took (0). want filled heart shown below each list item user has liked. problem is: if there many entries, there randomly filled hearts, if user liked 1 entry. can figure out mistake?
activity, saves in sqlite database if entry 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()) { { 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, sets elements of listview , changes imgage heart_filled if entry 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()) { { 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 + " ? , " + dbh.likes_entryid + " ? "; string args[] = {markerid.tostring(), objectid}; cursor cursor = dbase.query(dbh.table_likes, columns, selection, args , null, null, null, null); return cursor; }
maybe should add sqlite entries seem added correctly, because if entry heart filled mistake, not unfill it, keeps filled , increases number of likes. can "dislike" , heart unfilled again.
first idea: if have 1 , 0 dislike, change query if/else instead of if/else if; that's 1 possible error source fewer.
second idea update: posted comment under post, not experienced enough these things make clear statement now.
Comments
Post a Comment