Q:

Android, Adapter adding extra element to array?

I have an adapter that create 16 buttons in a gradview, as the buttons are being created im give then a tag and add it to an array, for some reason when i print the log it shows me 17 elements...any ideas why it contains 17 elements when i 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 a = 16;// HOW MANY TILES WILL THE ADAPTER DISPLAY AND CREATE IN
                    // THE GRID VIEW, REFRENCED IN THE 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 to random number

            // give each button a 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 for each button
            if (r == 5) {

                button.setBackgroundColor(yellow);
                button.setText("" + r4);// set text to 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;

A:

I have found the cause of the issue: GridView always creates 1 extra view, no matter whether all views are visible or not.
Example when all 16 views are visible:

At first it created and displayed 16 views (the last view is red 1). Then it created 1 extra view (view 17, red 2) which will never be displayed.
I don't know why GridView does this. Someone from the Android team decided to implement this control in such vague way.
As to your adapter, I would suggest that you populate your colorsArray in the constructor and don't change it in other places. Than you will be sure that it contains no more than 16 items.

