GridView getChildAt returning null for visible children

Viewed 453

I have a grid of images that were taken from a custom camera activity. On other activity I load the images into a grid. I have a method that turn on & off a checkbox on each Image. While trying to move through all the images, I'm getting some strange behavior: GridView.getCount() returns the correct number of images in the GridView, but GridView.getChildAt() method returned only part of the images. so, when I try to do so, I get a null Exception.

The GridView Adapter:

public class PhotoGalleryAdapter extends ArrayAdapter {

    // Declare variables
    private Activity adapterActivity;
    private int layoutResourceId;
    private ArrayList gridItemsArray;
    private static LayoutInflater inflater = null;

    public PhotoGalleryAdapter(Activity a, int layoutResourceId, ArrayList fGridItemArray) {
        super(a, layoutResourceId, fGridItemArray);
        this.layoutResourceId = layoutResourceId;
        this.adapterActivity = a;
        this.gridItemsArray = fGridItemArray;
    }

    public int getCount() {
        return gridItemsArray.size();
    }

    public Object getItem(int position) {
        return gridItemsArray.get(position);
    }

    public long getItemId(int position) {
        return position;
    }

    public View getView(final int position, View convertView, ViewGroup parent) {
        HolderViewRow holderRow = null;
        View rowView = (View) convertView;

        if (rowView == null){
            LayoutInflater inflater = adapterActivity.getLayoutInflater();

            rowView = inflater.inflate(layoutResourceId, parent, false);

            holderRow = new HolderViewRow();

            holderRow.imageFileBMP = (ImageView) rowView.findViewById(R.id.imageObject);

            holderRow.checkBox = (CheckBox) rowView.findViewById(R.id.itemCheckBox);
            holderRow.checkBoxImage = (ImageView) rowView.findViewById(R.id.itemImageCheckBox);

            rowView.setTag(holderRow);
        }
        else
        {
            holderRow = (HolderViewRow) rowView.getTag();
        }

        PhotoGalleryItem item = (PhotoGalleryItem) gridItemsArray.get(position);

        // Set the decoded bitmap iitemnto ImageView
        holderRow.imageFileBMP.setImageBitmap(item.getImageBMP());
        holderRow.imageFilePath = item.getImagePath();

        return rowView;
    }

    static class HolderViewRow {
        ImageView imageFileBMP;
        String imageFilePath;
        CheckBox checkBox;
        ImageView checkBoxImage;
    }
}


public class PhotoGalleryItem {
    private Bitmap imageBMP;
    private String imagePath;
    private CheckBox checkBox;
    private ImageView checkBoxImage;

    public PhotoGalleryItem(Bitmap imageBMP, String imagePath) {
        super();
        this.imageBMP = imageBMP;
        this.imagePath = imagePath;
    }

    public Bitmap getImageBMP() {
        return imageBMP;
    }

    public void setImageBMP(Bitmap imageBMP) {
        this.imageBMP = imageBMP;
    }

    public String getImagePath() {
        return imagePath;
    }

    public void setImagePath(String imagePath) {
        this.imagePath = imagePath;
    }
}

The Grid Activity:

        private void RenderGridAndSetImages() {
            // Locate the GridView in gridView_PhotoDisplay.xml
            gridView = (GridView) findViewById(R.id.gridView_PhotoDisplay);
            // Pass String arrays to Adapter Class
            imageAdapter = new PhotoGalleryAdapter(this, R.layout.gridview_photo_item, GridItemArrayList);
            gridView.setAdapter(imageAdapter);
    }

And the function that has the problem:

    public void SetVisibilityCheckBox_OnGrid (boolean checkBoxVisibility) {
        // Handle item selection On GRIDVIEW ImageItem
        final int size = gridView.getCount();
        try {
            ImageView imageCB;
            int cbVisibility;

            if (checkBoxVisibility)
                cbVisibility = View.VISIBLE;
            else
                cbVisibility = View.INVISIBLE;

            for(int i = 0; i < size; i++) {
                View viewItem = (View) gridView.getChildAt(i);
                if (viewItem != null) {
                    imageCB = (ImageView) viewItem.findViewById(R.id.itemImageCheckBox);
                    imageCB.setVisibility(cbVisibility);
                }
            }
        }
        catch (Exception ex) {
            // Error occurred while SetSelectCheckBoxOnGrid
            Log.e(PACKAGE_NAME, ACTIVITY_NAME + ".SetVisibilityCheckBox_OnGrid\n" + ex.getMessage());
        }
    }

The GridView Layout:

<RelativeLayout
    android:id="@+id/layout_Holder"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    android:background="@color/activityBackground"
    tools:context="com.mountain.saymera.PhotoGalleryActivity">

    <!-- Image view: layout_GridView -->
    <RelativeLayout
        android:id="@+id/layout_PhotoGridDisplay"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:orientation="horizontal"
        android:background="@color/activityBackground"
        android:padding="1dp">

        <GridView
            android:id="@+id/gridView_PhotoDisplay"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:columnWidth="100dp"
            android:drawSelectorOnTop="true"
            android:numColumns="auto_fit"
            android:verticalSpacing="1dp"
            android:horizontalSpacing="1dp"
            android:stretchMode="columnWidth"
            android:gravity="center"
            android:focusable="true"
            android:clickable="true"/>

        <TextView
            android:id="@+id/textView_NoImages"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/gallery_no_images"
            android:layout_centerVertical="true"
            android:layout_centerHorizontal="true"
            android:background="#00000000"
            android:textColor="@color/MenuButtonTint"
            android:textStyle="bold" />
    </RelativeLayout>
</RelativeLayout>

The GridView Item Layout:

<RelativeLayout
    android:id="@+id/layout_Holder"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:background="@drawable/single_photo_frame_on_grid">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center_vertical"
        android:orientation="vertical"
        android:background="@color/Grid_ItemFrame"
        android:padding="0.3dp">

        <com.mountain.saymera.SquareImageView
            android:id="@+id/imageObject"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:adjustViewBounds="true"
            android:scaleType="centerCrop"/>
    </LinearLayout>

    <CheckBox
        android:id="@+id/itemCheckBox"
        android:layout_width="20dp"
        android:layout_height="20dp"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true"
        android:visibility="invisible"
        android:tint="@color/Grid_ItemCheckbox"/>

    <ImageView
        android:id="@+id/itemImageCheckBox"
        android:layout_width="25dp"
        android:layout_height="25dp"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true"
        android:visibility="invisible"
        android:background="#00000000"
        android:src="@mipmap/ic_crop_square_black_24dp"
        android:tint="@color/MenuCheckboxTint"/>

</RelativeLayout>

Need some help, Thanks

1 Answers

I have faced the same issue with programatically added ImageViews in a LinearLayout. Unfortunately, this method seems to be buggy at the moment, so I had to help myself by finding an alternative. As a workaround, I saved the references of my ImageViews in a List and worked with them.

Related