Search code examples
androidfiltermultiple-choice

How to work out MultipleChoice filters on Listview using checkbox


I have different categories of filters like id, name, gender, area, caste, religion. Now i want to use multiple choice filters showing checkboxes, how can i work out with multiple choice filters because there are many kind of filters so permutation & combination is worst for it. currently i am done with single selection of filter (1 checkbox true at a time).


Solution

  • String str[] = new String[('Z' - 'A') + 1];
    boolean selectedItems[] = new boolean[str.length];
    protected ArrayList<CharSequence> selectedChars = new ArrayList<CharSequence>();
    private AlertDialog alertBox;
    
    private void showSpinner() {
    
        AlertDialog.Builder alertBuilder = new AlertDialog.Builder(this);
    
        int a = 0;
        for (char c = 'A'; c < 'Z'; c++) {
            str[a++] = String.valueOf(c);
        }
        boolean[] checkedChars = new boolean[str.length];
        int count = str.length;
        for (int i = 0; i < count; i++)
            checkedChars[i] = selectedChars.contains(str[i]);
    
        alertBuilder.setTitle("Select Country");
        alertBuilder.setCancelable(true);
        alertBuilder.setMultiChoiceItems(str, selectedItems,
                new OnMultiChoiceClickListener() {
    
                    @Override
                    public void onClick(DialogInterface dialog, int which,
                            boolean isChecked) {
                        if (isChecked)
                            selectedChars.add(str[which]);
                        else
                            selectedChars.remove(str[which]);
                    }
                });
    
        alertBox = alertBuilder.create();
        alertBox.show();
    }
    

    it will jst display the spinner with mulitple selection options.