Search code examples
androidgridviewandroid-arrayadapterandroid-checkbox

Item-Selection with Checkbox in Android-GridView


I've come to a problem with the Android GridView. I already tried various solutions from Stackoverflow but none of them actually worked.

I'm populating a GridView with the help of an ArrayAdapter. The Adapter inflates a layout containing only an image and a checkbox. I'm using that "convertView"-thing in order to prevent flickering when the images get added (nearly 500 images are getting added to the GridView).

The Problem: When I check a checkbox and scroll, the checked-state disappears. Also, when I for instance check multiple GridView item's checkboxes, the pattern of selection endlessly continues for all the following elements.


My GridView is the child-element of a RelativeLayout. The RelativeLayout is used as the root element of a fragment being used in a ViewPager:

<GridView
    android:id="@+id/MainGrid"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/appBackgroundColor"
    android:drawSelectorOnTop="false"
    android:listSelector="#00000000"
    android:numColumns="2"
    android:paddingLeft="6dp"
    android:paddingTop="5dp"
    android:paddingRight="6dp"
    android:translationZ="2dp" />

I'm populating the GridView with the help of a thread, which is downloading image-data from a website. I think there shouldn't be a problem with this, but for completeness, here is the code:

//Declaring the Array
ArrayList<GridImage> ImageArray = new ArrayList<>();

//And the adapter
Adapter_GridAdapter GridViewAdapter = new Adapter_GridAdapter (getActivity().getApplicationContext(), ImageArray );

//The following is executed 500 times in a thread
ImageArray.add(new GridImage(URL));
getActivity().runOnUiThread(new Runnable() {
    @Override
    public void run() {
        GridViewAdapter.notifyDataSetChanged();
    }
});

The code of the GridView adapter:

public class Adapter_GridAdapter extends ArrayAdapter<GridImage> {

Context context;

public Adapter_GridAdapter (Context context, ArrayList<GridImage> SearchResults) {
    super(context, 0, SearchResults);
    this.context = context;
}

@Override
public View getView(final int position, View convertView, ViewGroup parent) {

    LayoutInflater inflater = LayoutInflater.from(getContext());
    View view = convertView;
    if (convertView == null) {
        view = inflater.inflate(R.layout.griditem_ig_post, null);

        final ImageView ThisViewIMG = view.findViewById(R.id.GridImageView);

        //Calculating Image Height (Square Image)
        ThisViewIMG.addOnLayoutChangeListener(new View.OnLayoutChangeListener() {
            @Override
            public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) {
                ThisViewIMG.removeOnLayoutChangeListener(this);
                ThisViewIMG.getLayoutParams().height = ThisViewIMG.getWidth();
                ThisViewIMG.requestLayout();
            }
        });

    }

    final GridImage ThisImg = getItem(position);
    final ImageView ThumbnailImage = view.findViewById(R.id.GridImageView);

    //THIS IS THE CUSTOM CHECKBOX!
    final CustomCheckBox scb = (CustomCheckBox) view.findViewById(R.id.MyCheckBox);

    //I AM SAVING THE VALUE IN THE OBJECT (TRY #38181 THAT DIDN'T WORK)
    scb.setChecked(ThisImg.IsChecked);
    scb.setOnCheckedChangeListener(new CustomCheckBox.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CustomCheckBox checkBox, boolean isChecked) {
            Log.d("CustomCheckBox", String.valueOf(isChecked));
            ThisImg.setChecked(isChecked);
        }
    });
Glide.with(context).load(Post.IMG_ThumbnailURL).fitCenter().into(ThumbnailImage);


    return view;
}
}

The Object itself is quite simple, the problem (i think) also shouldn't hide in here:

public class GridImage{
    public String PostURL;
    public Boolean IsChecked = false;

    GridImage(String URL){
        PostURL = URL;
    }

    public void setChecked(Boolean checked) {
        IsChecked = checked;
    }
}

What I already tried:


Solution

  • When doing with ListView/GridView adapters, I always avoid to have final and onCheckedChangeListener because both may have problem sometimes even the logic seems right, so try the code below:

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
    
        LayoutInflater inflater = LayoutInflater.from(getContext());
        View view = convertView;
        if (convertView == null) {
            view = inflater.inflate(R.layout.griditem_ig_post, null);
    
            ImageView ThisViewIMG = view.findViewById(R.id.GridImageView);
    
            //Calculating Image Height (Square Image)
            ThisViewIMG.addOnLayoutChangeListener(new View.OnLayoutChangeListener() {
                @Override
                public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) {
                    v.removeOnLayoutChangeListener(this);
                    v.getLayoutParams().height = v.getWidth();
                    v.requestLayout();
                }
            });
    
        }
    
        GridImage ThisImg = getItem(position);
        ImageView ThumbnailImage = view.findViewById(R.id.GridImageView);
    
        //THIS IS THE CUSTOM CHECKBOX!
        CustomCheckBox scb = (CustomCheckBox) view.findViewById(R.id.MyCheckBox);
    
        scb.setChecked(ThisImg.IsChecked);
        scb.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                CustomCheckBox cb = (CustomCheckBox)view;
                int position = (int)cb.getTag();
                GridImage ThisImg = getItem(position);
                Log.d("CustomCheckBox", String.valueOf(!ThisImg.IsChecked));
                ThisImg.setChecked(!ThisImg.IsChecked);
                cb.setChecked(ThisImg.IsChecked);
            }
        });
        Glide.with(context).load(Post.IMG_ThumbnailURL).fitCenter().into(ThumbnailImage);
    
        scb.setTag(position);
        return view;
    }
    

    Hope that helps!