android - How to update row in CursorAdapter properly -
i have trouble cursoradapter data updating.
when press on "star" img of listview row item needs changes in db column "isfav" (0 1 , vice versa) , show completed star or free star(favorite checkbox e.g.). when pressing on star - current row star not updating, last of showed rows affected, press on it. screenshot: http://tinypic.com/r/2eph8uf/5
the following code placing below:
private class myadapter extends cursoradapter { private layoutinflater mlayoutinflater; public myadapter(context context, cursor c, boolean autorequery) { super(context, c, autorequery); mcontext = context; mlayoutinflater = layoutinflater.from(context); } news tmp = new news(0,0,null,null,0,0); @override public void bindview(view v, context context, cursor c) { tmp.setname(c.getstring(c.getcolumnindexorthrow(dbhelper.name))); tmp.setdescr(c.getstring(c.getcolumnindexorthrow(dbhelper.description))); tmp.setpubdate(c.getlong(c.getcolumnindexorthrow(dbhelper.date))); tmp.setguid(c.getint(c.getcolumnindexorthrow(dbhelper.uid))); tmp.setisfav(c.getint(c.getcolumnindexorthrow(dbhelper.is_fav))); tmp.setisread(c.getint(c.getcolumnindexorthrow(dbhelper.is_read))); textview title_text = (textview) v.findviewbyid(r.id.text); if (title_text != null) { title_text.settext(tmp.getname() + " " + convertlongtodate(tmp.getpubdate())); } title_text.setonclicklistener(new onclicklistener() { @override public void onclick(view v) { intent _intent = new intent(getactivity(), itemactivity.class); _intent.putextra("title", tmp.getname() + " " + long.tostring(tmp.getpubdate())); _intent.putextra("desc", tmp.getdescr()); startactivity(_intent); if (tmp.getisread() == 1) return; tmp.setisread(1); //working wrong! contentvalues cv = getcontvalues(tmp); loader.update(dbhelper.database_name, cv, dbhelper.uid + "=" + cv.getasinteger("_id"), null); // db.updateitem(db.getwritabledatabase(), tmp); // getloadermanager().initloader(0, null, mcallbacks); } }); final imageview fav_img = (imageview) v.findviewbyid(r.id.image); if (fav_img != null) { if (tmp.getisfav() != 0) { fav_img.setimageresource(r.drawable.fav); } else fav_img.setimageresource(r.drawable.unfav); } fav_img.setonclicklistener(new onclicklistener() { @override public void onclick(view v) { if (tmp.getisfav() == 1) { tmp.setisfav(0); fav_img.setimageresource(r.drawable.unfav); } else { tmp.setisfav(1); fav_img.setimageresource(r.drawable.fav); } log.d("fav:", integer.tostring(tmp.getisfav()) + " id: " + integer.tostring(tmp.getguid())); // db.updateitem(db.getwritabledatabase(), tmp); // getloadermanager().restartloader(0, null, mcallbacks); //working wrong! contentvalues cv = getcontvalues(tmp); loader.update(dbhelper.database_name, cv, dbhelper.uid + "=" + cv.getasinteger("_id"), null); } }); linearlayout lay = (linearlayout) v.findviewbyid(r.id.item_layout); if (tmp.getisread() != 0) lay.setbackgroundcolor(color.gray); else lay.setbackgroundcolor(color.yellow); } private contentvalues getcontvalues(news tmp) { contentvalues cv = new contentvalues(); cv.put("_id", tmp.getguid()); cv.put("title", tmp.getname()); cv.put("pubdate", tmp.getpubdate()); cv.put("description", tmp.getdescr()); cv.put("isread", tmp.getisread()); cv.put("isfav", tmp.getisfav()); return cv; } @override public view newview(context context, cursor cursor, viewgroup parent) { view v = mlayoutinflater.inflate(r.layout.list_item, parent, false); return v; } private string convertlongtodate(long value) { locale l = new locale("ru"); //if (l.getcountry()) date d = new date(value); simpledateformat df = new simpledateformat("d m", l); return new string(df.format(d)); }
}
just adjustments:
final imageview fav_img = (imageview) v.findviewbyid(r.id.image); boolean is_favourite = false; if (fav_img != null) { if (tmp.getisfav() != 0) { fav_img.setimageresource(r.drawable.fav); } else fav_img.setimageresource(r.drawable.unfav); } fav_img.setonclicklistener(new onclicklistener() { @override public void onclick(view v) { if (is_favourite) // if row favourite { is_favourite=false; fav_img.setimageresource(r.drawable.unfav); } else { is_favourite= true; fav_img.setimageresource(r.drawable.fav); } //log.d("fav:", integer.tostring(tmp.getisfav()) + " id: " + integer.tostring(tmp.getguid())); // db.updateitem(db.getwritabledatabase(), tmp); // getloadermanager().restartloader(0, null, mcallbacks); //working wrong! contentvalues cv = getcontvalues(tmp); loader.update(dbhelper.database_name, cv, dbhelper.uid + "=" + cv.getasinteger("_id"), null); } });
Comments
Post a Comment