Question

I've spent over a week trying to figure out a way to do a Limited Multi Selection Preference list. Nothing I've tried works. I'm ready to give up on Android if something seemingly simple is so hard. I've been programming a long time and don't remember being beaten up this badly by something like this. I have to assume I am not understanding something basic. I hope someone can point me in the right direction.

Here is the simplest code I can think off that should work. It does not clear the checkbox even when setting it to false, I've tried true as well. Why doesn't that work? If that will not work, what will?

Any help would be most appreciated.

    @Override
    protected void onPrepareDialogBuilder(Builder builder) 
    {
     CharSequence[] entries = getEntries();
     CharSequence[] entryValues = getEntryValues();

        if (entries == null || entryValues == null || entries.length != entryValues.length ) {
            throw new IllegalStateException(
                    "ListPreference requires an entries array and an entryValues array which are both the same length");
        }

        // Added by WJT since we are loading the entries values after instantiation 
        // we need the clicked indexes to be setup now, they would not have been
        // set up in the constructor
        if ((mClickedDialogEntryIndices == null) || (mClickedDialogEntryIndices.length == 0))
         mClickedDialogEntryIndices = new boolean[getEntries().length];

        restoreCheckedEntries();
        builder.setMultiChoiceItems(entries, mClickedDialogEntryIndices, 
                new DialogInterface.OnMultiChoiceClickListener() 
        {
   public void onClick(DialogInterface dialog, int which, boolean val) 
   {
    mDlg = (AlertDialog)getDialog();
                mListView = (ListView)mDlg.getListView();
    if (val)
             {
              if (mSelectedCount < mLimit)
                 {
               mClickedDialogEntryIndices[which] = val;
                  mSelectedCount++;

                 }
              else
                 {
                  mListView.setItemChecked(which, false);
               Toast.makeText(getContext(),
              R.string.newsLimitExceededMessage,
              Toast.LENGTH_LONG).show();

                 }  // (mSelectedCount < mLimit)

             }
             else
             {
              mClickedDialogEntryIndices[which] = val;
              mSelectedCount--;

             }  // (val)

   }  // void onClick(DialogInterface dialog, int which, boolean val)


        });  //  DialogInterface.OnMultiChoiceClickListener() 

    }  // void onPrepareDialogBuilder(Builder builder) 

Thanks,

\ ^ / i l l

Was it helpful?

Solution

Here's how I would approach the problem:

Step #1: Get this working in a standalone throwaway test activity. Forget preferences. Forget dialogs. Just focus on the functionality of having a CHOICE_MODE_MULTIPLE ListView where, after a certain number of items are checked, the unchecked items become disabled.

Step #2: Get the functionality from Step #1 working in the form of a custom widget. By this, I mean you would implement a subclass of ListView (I guess...might be some container if there's more to it than a ListView) that bakes in all of what you need from Step #1.

Step #3: Create a custom DialogPreference subclass that uses the custom widget from Step #2.

For example, here is a sample project where I have a custom ColorMixer widget, rolled into a ColorPreference.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top