i have strange problem i've been trying solve last hours can't find whats wrong. have sqlite database containing names , small amount of text. listview gets populated names it, when click on listview see text connected names. have "add favorite" , when add favorite list displays on activity. displayed info correct in new/favorite listview (names, title , image) when click on wrong data displayed.
i have 10 rows in original listview , in "favorite" listview same order in first one. if add e.g movie..casablanca on position 7 in original list,it place self on first position in new listview (position 0) , shows correct actors, covers , author. when click see more info(starts new activity). shows info the original listview info movie on position 0.
but if click on context menu check it. displays correct data want see. strange , hope there know problem can be? i've tried lot of different combos , solutions arrays , strings/stringbuilder , on. last code wrote before came here.
public class favorites extends actionbaractivity { databasehelper dbhelper; listview favoriteslistview; cursor mycursor; string[] mystringarray = new string[3]; listviewcursoradapter myfavsadapter; string openbookcmeny = "Öppna boken"; string rmvfavscmeny = "ta bort från dina favoriter"; string websitecmeny = "klicka här för mer information på bokens hemsida"; string showwebsitecmeny; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_favorites); dbhelper = databasehelper.getinstance(this); mycursor = dbhelper.visafavoriter(); dbhelper.getwritabledatabase(); myfavsadapter = new listviewcursoradapter(this, mycursor); favoriteslistview = (listview)findviewbyid(r.id.favoriteslistview); favoriteslistview.setadapter(myfavsadapter); registerforcontextmenu(favoriteslistview); favoriteslistview.setonitemclicklistener(new adapterview.onitemclicklistener() { @override public void onitemclick(adapterview<?> parent, view view, int position, long id) { cursor c = (cursor)myfavsadapter.getitem(view.getid()); mystringarray[1]= c.getstring(4); intent = new intent(favorites.this, visaboken.class); i.putextra(null, mystringarray); startactivity(i); } }); } @override public void oncreatecontextmenu(contextmenu menu, view v, contextmenu.contextmenuinfo menuinfo) { super.oncreatecontextmenu(menu, v, menuinfo); adapterview.adaptercontextmenuinfo info = (adapterview.adaptercontextmenuinfo) menuinfo; cursor c = ((cursor) myfavsadapter.getitem(info.position)); string ctitle = c.getstring(2); menu.setheadertitle(ctitle); menu.add(0, v.getid(), 0, openbookcmeny); menu.add(0, v.getid(), 0, rmvfavscmeny); menu.add(0, v.getid(), 0, websitecmeny); } @override public boolean oncontextitemselected(menuitem item) { adapterview.adaptercontextmenuinfo info = (adapterview.adaptercontextmenuinfo)item.getmenuinfo(); cursor c = ((cursor) myfavsadapter.getitem(info.position)); mystringarray[0] = c.getstring(0); mystringarray[1] = c.getstring(4); showwebsitecmeny = c.getstring(6); if (item.gettitle() == openbookcmeny){ intent = new intent(favorites.this, visaboken.class); i.putextra(null, mystringarray); startactivity(i); } else if (item.gettitle() == rmvfavscmeny){ dbhelper.removeonefavorite(mystringarray[0]); recreate(); } else if (item.gettitle() == websitecmeny){ intent browserintent = new intent(intent.action_view, uri.parse(showwebsitecmeny)); startactivity(browserintent); } return super.oncontextitemselected(item); }
the class gets intents , info display it.
public class visaboken extends actionbaractivity implements adapterview.onitemclicklistener { actionbardrawertoggle mydrawertoggle; drawerlayout mydrawerlayout; listview mydrawerlistview; arraylist<drawermenuname> mydrawermenuname = new arraylist<>(); databasehelper dbhelper; public static final string col_id = "_id"; public static string tag = "checkidfromdb"; textview visabokentv; string[] mystringarray = new string[2]; string[] mystringarray2; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_visa_boken); visabokentv = (textview)findviewbyid(r.id.visabokentv); bundle extras = getintent().getextras(); mystringarray = extras.getstringarray(null); visabokentv.settext(mystringarray[1]); mydrawerlayout = (drawerlayout)findviewbyid(r.id.drawer_layout); dbhelper = databasehelper.getinstance(this); dbhelper.getwritabledatabase();
sorry not understanding explanation of problem. see code suspect. suggestion, change code in public void onitemclick
from:
cursor c = (cursor)myfavsadapter.getitem(view.getid());
to:
cursor c = (cursor)myfavsadapter.getitem( position );
normally, use getitem(), pass position in listview, starting value 0 top. cannot use view.getid()
unless there id set various view objects don't think did that, sounds tricky. perhaps should post adapter (myfavsadapter
) code.
let's try first until can see other issue.
Comments
Post a Comment