views:

20

answers:

0

In Android, I have been attempting to add functionality to a multiple choice list that pops up in a dialog. (see http://blog.350nice.com/wp/wp-content/uploads/2009/07/listpreferencemultiselect.java) The issue is that I want to limit the number of choices to 3. Say I have a list of US States in my list, and I only want someones 3 favorite states. The code that I have been attempting to tweak is:

@Override
protected void onPrepareDialogBuilder(Builder builder) {
    ...
    restoreCheckedEntries();//puts true back into mClickedDialogEntry boolean indices.
    builder.setMultiChoiceItems(entries, mClickedDialogEntryIndices, 
            new DialogInterface.OnMultiChoiceClickListener() {
                public void onClick(DialogInterface dialog, int which, boolean val) {
                    mClickedDialogEntryIndices[which] = val;
                }
    });

Basically this class extends ListPreference, and overrides some of the methods. It uses a string with separators in order to store the selected values from the multichoice list. Then when it is called it reads those stored values back into this 'mClickedDialogEntryIndices' boolean array for display if they are equal to anything in the entryValues list.

Right now I can tell how many are selected by simply going through this boolean after each onClick and counting the number of 'true' indexes. My current tweaks to this look roughly like the following:

builder.setMultiChoiceItems(entries, mClickedDialogEntryIndices, 
            new DialogInterface.OnMultiChoiceClickListener() {
                public void onClick(DialogInterface dialog, int which, boolean val) {
                    if(getNumberChecked()<=3){
                        mClickedDialogEntryIndices[which] = val;
                    }else{
                        Toast.makeToast(context, "hey you cant select more then 3"...);
                        dialog.dismiss();
                    }

                }
    });

Essentially I have the window close after selecting more then 3. This however is not what I want. I would really like a way to disable all the other icons other then the 3 that are selected (once 3 are selected), or not allow selections of further CheckedTextViews (i.e. don't change the icon if they are clicked). If there is a better way to implement a dialog multichoice checkbox I am all ears as well. Sorry for the long winded question, and thanks in advance for any help!