dolphinately
dolphinately

Reputation: 69

How to check all checkbox in gridview using another checkbox or a button

I know this question is familiar but i need help. I don't know why my code returns a null pointer. here I got so far:

    final GridView imagegrid = (GridView) findViewById(R.id.grid_GalleryImage);
    imageAdapter = new ImageAdapter();
    imagegrid.setAdapter(imageAdapter);

    // Check All

    Button btnCheckAll = (Button) findViewById(R.id.btn_SelectAll);
    btnCheckAll.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                int count = imagegrid.getAdapter().getCount();
                for (int i = 0; i < count; i++) {
                    LinearLayout itemLayout = (LinearLayout)imagegrid.getChildAt(i); // Find by under LinearLayout
                    CheckBox checkbox = (CheckBox)itemLayout.findViewById(R.id.cbo_CheckImage);
                    checkbox.setChecked(true);
                } 
            }
    });

Upvotes: 0

Views: 1655

Answers (1)

sonali8890
sonali8890

Reputation: 41

Inside Adapter class change getView as:

public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
LayoutInflater in = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView =  in.inflate(R.layout.inflator, null);
CheckBox c = (CheckBox) convertView.findViewById(R.id.checkBox);
c.setId(position);

return convertView;

}

And inside onClickListener:

btnCheckAll.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {

            int count = imagegrid.getAdapter().getCount();

            for (int i = 0; i < count; i++) {
              CheckBox c = (CheckBox) gridView.findViewById(i);
              c.setChecked(true); 
            }
});

Upvotes: 1

Related Questions