java - Android, Adapter adding extra element to array? -
i have adapter create 16 buttons in gradview, buttons being created im give tag , add array, reason when print log shows me 17 elements...any ideas why contains 17 elements when create 16 buttons...
public class buttonadapter extends baseadapter { private context mcontext; arraylist<string> colorsarray = new arraylist<string>(0);// add button tags public buttonadapter(context c) { mcontext = c; } public int getcount() { int = 16;// how many tiles adapter display , create in // grid view, refrenced in oncreate method below return a; } public object getitem(int position) { return null; } public long getitemid(int position) { return 0; } public view getview(int position, view convertview, viewgroup parent) { final button button; if (convertview == null) { int r = 1 + (int) (math.random() * 5); button = new button(mcontext); button.setlayoutparams(new gridview.layoutparams(100, 100)); button.settext("" + r);// set text random number // give each button new reference number int r4 = 1 + (int) (math.random() * 5); int r3 = 1 + (int) (math.random() * 4); int r2 = 1 + (int) (math.random() * 3); int r1 = 1 + (int) (math.random() * 2); // setup attributes each button if (r == 5) { button.setbackgroundcolor(yellow); button.settext("" + r4);// set text new random number colorsarray.add("yellow"); button.settag("yellow"); } else if (r == 4) { button.setbackgroundcolor(green); button.settext("" + r3); colorsarray.add("green"); button.settag("green"); } else if (r == 3) { button.setbackgroundcolor(red); button.settext("" + r2); colorsarray.add("red"); button.settag("red"); } else if (r == 2) { button.setbackgroundcolor(blue); button.settext("" + r1); colorsarray.add("blue"); button.settag("blue"); } else if (r == 1) { button.setbackgroundcolor(purple); colorsarray.add("purple"); button.settag("purple"); } else { button.setbackgroundcolor(white); button.settext("0"); } log.d(tag, colorsarray.tostring()); } else { button = (button) convertview; } return button;
i have found cause of issue: gridview creates 1 view, no matter whether views visible or not.
example when 16 views visible:
at first created , displayed 16 views (the last view red 1). created 1 view (view 17, red 2) never displayed.
i don't know why gridview this. android team decided implement control in such vague way.
as adapter, suggest populate colorsarray
in constructor , don't change in other places. sure contains no more 16 items.
Comments
Post a Comment