androidgridviewcheckboxdialogbaseadapter

Checking checkboxes inside GridView, using custom BaseAdapter


Good morning/afternoon/evening/night,

While working on my project, I came across a problem and I doesn't seem to be able to figure it out. I want to create a dialog where a user is able to check several items and finish with an 'add' button.

I've created a custom baseadapter and a custom dialog with a gridview in it. the dialog also contains the 'ADD' button. My goal is to be able to check which items are checked.

The layout for the BaseAdapter has some text, image and the checkbox in it.

Constructor for the dialog:

public Custom_dialog_add(Context context, List<Oefening> oefeningList) {
    super(context);
    this.requestWindowFeature(Window.FEATURE_NO_TITLE);
    this.setContentView(R.layout.custom_dialog_add);

    gridView_add = (GridView) findViewById(R.id.custom_dialog_add_gridview);
    imageAdapter_add = new ImageAdapter_Add(getContext(), oefeningList);
    gridView_add.setAdapter(imageAdapter_add);
    imageAdapter_add.notifyDataSetChanged();

    // This is the add button
    Button add = (Button) findViewById(R.id.custom_dialog_add_button);
    add.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            //Need something to do here 

        }
    });
}

The getView from the BaseAdapter:

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

    LayoutInflater inflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    View gridView;

    if (convertView == null) {

        gridView = new View(context);

        gridView = inflater.inflate(R.layout.custom_adapter_add, null);

        // set value into textview
        TextView textView = (TextView) gridView
                .findViewById(R.id.custom_adapter_add_textview);
        textView.setText(oefeningen.get(position).getName());

        //Checkbox
        final CheckBox checkBox = (CheckBox) gridView.findViewById(R.id.custom_adapter_add_checkbox);
        checkBox.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (checkBox.isChecked()){

                }else{
                }
            }
        });

        // set image based on selected text
        ImageView imageView = (ImageView) gridView
                .findViewById(R.id.custom_adapter_add_image);

    } else {
        gridView = (View) convertView;
    }
    return gridView;
}

I've found some work-around-solutions online, like saving things in SharedPrefferences but there must be a more efficient and clean way... If more code of explanation is needed, let me know.

Any help will be appreciated!

Thanks


Solution

  • You can put this in your Button CLickListener:

    checkGridView(gridView); //declare the GridView ad final that you could call it inside the listener
    

    And define this method:

    private void checkGridView(GridView gridView) {
        for (int i = 0; i < gridView.getChildCount() ; i++ ){
            View v = gridView.getChildAt(i);
            CheckBox checkBox = (CheckBox) v.findViewById(R.id.custom_adapter_add_checkbox);
            boolean itemChecked = checkBox.isChecked();
        }
    }