Bonjour,

J'ai une classe (voir code ci-dessous) qui me permet de cocher plusieurs choix dans une ListPreferencres ( par défaut on ne peut en sélectionné que 1, sauf à partir de l'API 11).

Cependant j'aimerais peaufiner le code. J'aimerais créer une méthode qui me permettrai d'effectuer la tâche suivante : si l'une des valeurs cochées est = "ALL" alors on sélectionne toutes les checkbox de ma liste. Pourriez vous m'aider ? Merci par avance.


Voici ma classe

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
 
package com.preferences;
 
 
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.preference.ListPreference;
import android.util.AttributeSet;
import android.widget.Toast;
 
/**
 * A {link Preference} that displays a list of entries as
 * a dialog and allows multiple selections
 * <p>
 * This preference will store a string into the SharedPreferences. This string will be the values selected
 * from the {@link #setEntryValues(CharSequence[])} array.
 * </p>
 */
 
public class ListPreferenceMultiSelect extends ListPreference {
    //Need to make sure the SEPARATOR is unique and weird enough that it doesn't match one of the entries.
    //Not using any fancy symbols because this is interpreted as a regex for splitting strings.
    private static final String SEPARATOR = "OV=I=XseparatorX=I=VO";
 
    private boolean[] mClickedDialogEntryIndices;
 
    public ListPreferenceMultiSelect(Context context, AttributeSet attrs) {
        super(context, attrs);
 
        mClickedDialogEntryIndices = new boolean[getEntries().length];
    }
 
    @Override
    public void setEntries(CharSequence[] entries) {
        super.setEntries(entries);
        mClickedDialogEntryIndices = new boolean[entries.length];
    }
 
    public ListPreferenceMultiSelect(Context context) {
        this(context, null);
    }
 
    @Override
    protected void onPrepareDialogBuilder(AlertDialog.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");
        }
 
        restoreCheckedEntries();
        builder.setMultiChoiceItems(entries, mClickedDialogEntryIndices,
                new DialogInterface.OnMultiChoiceClickListener() {
                    public void onClick(DialogInterface dialog, int which, boolean val) {
                        mClickedDialogEntryIndices[which] = val;
 
 
 
                    }
                });
    }
 
    public static String[] parseStoredValue(CharSequence val) {
        if ( "".equals(val) )
            return null;
        else
            return ((String)val).split(SEPARATOR);
    }
 
    private void restoreCheckedEntries() {
        CharSequence[] entryValues = getEntryValues();
 
        String[] vals = parseStoredValue(getValue());
        if ( vals != null ) {
            for ( int j=0; j<vals.length; j++ ) {
                String val = vals[j].trim();
                for ( int i=0; i<entryValues.length; i++ ) {
                    CharSequence entry = entryValues[i];
                    if ( entry.equals(val) ) {
                        mClickedDialogEntryIndices[i] = true;
                        break;
                    }
                }
            }
        }
    }
 
    @Override
    protected void onDialogClosed(boolean positiveResult) {
//        super.onDialogClosed(positiveResult);
 
        CharSequence[] entryValues = getEntryValues();
        if (positiveResult && entryValues != null) {
            StringBuffer value = new StringBuffer();
            for ( int i=0; i<entryValues.length; i++ ) {
                if ( mClickedDialogEntryIndices[i] ) {
                    value.append(entryValues[i]).append(SEPARATOR);
                }
            }
 
            if (callChangeListener(value)) {
                String val = value.toString();
                if ( val.length() > 0 )
                    val = val.substring(0, val.length()-SEPARATOR.length());
                setValue(val);
            }
        }
    }
}