i'm using spinner custom adapter. issue when scrolling spinner, spinner's values changed other position's value, screenshot : screenshot
my layout not contains listview. don't know why happens. codes :
#teamscreen.java# public class teamscreen extends fragment { teamlistadapter teamlistadapter; list<hashmap<string, string>> teamlist; spinner spin_list ; // instance private static teamscreen instance; public static teamscreen getinstance() { if(instance == null) instance = new teamscreen(); return instance; } @nullable @override public view oncreateview(layoutinflater inflater, @nullable viewgroup container, @nullable bundle savedinstancestate) { view v = inflater.inflate(r.layout.layout_team, container, false); spin_list = (spinner) v.findviewbyid(r.id.spin_list); refreshteamlist(); return v; } public void refreshteamlist() { string sql = "select list_no " + " , list_name " + " , img0 " + " , img1 " + // img0 ~ img5 number, between 0 , 5 " , img2 " + " , img3 " + " , img4 " + " , img5 " + " table_test"; // dbmng.selectquerys return result above query, list<hashmap<string, string>> teamlist = dbmng.selectquerys(getactivity(), sql, null); if(teamlist != null) { teamlistadapter = new teamlistadapter(getactivity(), android.r.layout.simple_spinner_dropdown_item, teamlist); spin_list.setadapter(teamlistadapter); teamlistadapter.notifydatasetchanged(); } } } #teamlistadapter.java# public class teamlistadapter extends arrayadapter<hashmap<string, string>> { context ctx; listlist<hashmap<string, string>> objects; layoutinflater inflater; viewholder vh; public teamlistadapter(context context, int resource, list<hashmap<string, string>> objects) { super(context, resource, objects); this.ctx = context; this.objects = objects; this.inflater = layoutinflater.from(context); } @override public view getdropdownview(int position, view v, viewgroup parent) { return getcustomview(position, v, parent); } @override public view getview(int position, view v, viewgroup parent) { return getcustomview(position, v, parent); } public view getcustomview(int position, view convertview, viewgroup parent) { view row = convertview; if(row == null) { row = inflater.inflate(r.layout.item_team, parent, false); vh = new viewholder(row); row.settag(vh); } else vh = (viewholder) row.gettag(); hashmap<string, string> item = objects.get(position); vh.tv_name.settext(item.get("list_name")); for(int i=0;i<6;i++) { if(!item.getstring("img" + i).trim().equals("")) { // image file name likes "test0.png", "test1.png", .... "test5.png" int imgid = ctx.getresources().getidentifier( "test" + item.get("img" + i), "drawable", ctx.getpackagename()); if(imgid != 0) glide.with(ctx).load(imgid).into(vh.imgs[i]); } } return row; } class viewholder { imageview imgs[]; textview tv_name; public viewholder(view v) { imgs = new imageview[6]; imgs[0] = (imageview) v.findviewbyid(r.id.img_team0); imgs[1]= (imageview) v.findviewbyid(r.id.img_team1); imgs[2] = (imageview) v.findviewbyid(r.id.img_team2); imgs[3] = (imageview) v.findviewbyid(r.id.img_team3); imgs[4] = (imageview) v.findviewbyid(r.id.img_team4); imgs[5] = (imageview) v.findviewbyid(r.id.img_team5); tv_name = (textview) v.findviewbyid(r.id.tv_name); } } }
cause using
convertview
,item 9 same item 1.for issue ,you can :for(int i=0;i<6;i++){ if(!item.getstring("img" + i).trim().equals("")) { ... //load image using glide if has imgid glide.with(ctx).load(imgid).into(vh.imgs[i]); }else{ //do can clear image convertview , set default image. vh.imgs[i].setimageresource(r.drawable.defaultimage); } }
Comments
Post a Comment