1

J'essaie de désélectionner une fois que la limite dépasse. Voici ci-dessous le code, je suis en mesure d'atteindre le toast lorsque la limite de sélection maximale dépasse.Mais quand je Je suis en train d'essayer de désactiver ou de désélectionner l'élément lorsque les limites dépassent, cela ne fonctionne pas.comment désactiver la sélection de l'élément une fois que la limite dépasse dans MultiSelection Spinner Android

public class MultiSelectionSpinner1 extends Spinner implements 
     OnMultiChoiceClickListener { 

    public interface OnMultipleItemsSelectedListener{ 
     void selectedIndices(List<Integer> indices); 
     void selectedStrings(List<String> strings); 
    } 
    private OnMultipleItemsSelectedListener listener; 

    String[] _items = null; 
    boolean[] mSelection = null; 
    boolean[] mSelectionAtStart = null; 
    String _itemsAtStart = null; 

    ArrayAdapter<String> simple_adapter; 

    public MultiSelectionSpinner1(Context context) { 
     super(context); 
     simple_adapter = new ArrayAdapter<>(context, 
       android.R.layout.simple_spinner_item); 
     super.setAdapter(simple_adapter); 
    } 

    public MultiSelectionSpinner1(Context context, AttributeSet attrs) { 
     super(context, attrs); 

     simple_adapter = new ArrayAdapter<>(context, 
       android.R.layout.simple_spinner_item); 
     super.setAdapter(simple_adapter); 
    } 

    public void setListener(OnMultipleItemsSelectedListener listener){ 
     this.listener = listener; 
    } 

    public void onClick(DialogInterface dialog, int which, boolean isChecked) { 
     if (mSelection != null && which < mSelection.length) { 
      Log.e("_items.length", "_items.length" + _items.length); 
      Log.e("_itemsAtStart", "_itemsAtStart" + _itemsAtStart); 


//   listener.selectedStrings(getSelectedStrings()); 
      if(getSelectedIndices().size()<3){ 
       mSelection[which] = isChecked; 
       simple_adapter.clear(); 
       simple_adapter.add(buildSelectedItemString()); 
      }else 
      { 
       Toast.makeText(getContext(),"Exceeds",Toast.LENGTH_SHORT).show(); 
       setDiselection(which); 

      } 
      //listener.selectedIndices(getSelectedIndices()); 
     } else { 
      throw new IllegalArgumentException(
        "Argument 'which' is out of bounds."); 
     } 
    } 


    @Override 
    public boolean performClick() { 
     AlertDialog.Builder builder = new AlertDialog.Builder(getContext()); 
     builder.setTitle("Please select!!!"); 
     builder.setMultiChoiceItems(_items, mSelection, this); 
     _itemsAtStart = getSelectedItemsAsString(); 
     builder.setPositiveButton("Submit", new DialogInterface.OnClickListener() { 
      @Override 
      public void onClick(DialogInterface dialog, int which) { 
       System.arraycopy(mSelection, 0, mSelectionAtStart, 0, mSelection.length); 
       listener.selectedIndices(getSelectedIndices()); 
       listener.selectedStrings(getSelectedStrings()); 
      } 
     }); 
     builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { 
      @Override 
      public void onClick(DialogInterface dialog, int which) { 
       simple_adapter.clear(); 
       simple_adapter.add(_itemsAtStart); 
       System.arraycopy(mSelectionAtStart, 0, mSelection, 0, mSelectionAtStart.length); 
      } 
     }); 
     builder.show(); 
     return true; 
    } 

    @Override 
    public void setAdapter(SpinnerAdapter adapter) { 
     throw new RuntimeException(
       "setAdapter is not supported by MultiSelectSpinner."); 
    } 

    public void setItems(String[] items) { 
     _items = items; 
     mSelection = new boolean[_items.length]; 
     mSelectionAtStart = new boolean[_items.length]; 
     simple_adapter.clear(); 
     simple_adapter.add(_items[0]); 
     Arrays.fill(mSelection, false); 
     mSelection[0] = true; 
     mSelectionAtStart[0] = true; 
    } 

    public void setItems(List<String> items) { 
     _items = items.toArray(new String[items.size()]); 
     mSelection = new boolean[_items.length]; 
     mSelectionAtStart = new boolean[_items.length]; 
     simple_adapter.clear(); 
     simple_adapter.add(_items[0]); 
     Arrays.fill(mSelection, false); 
     mSelection[0] = true; 
    } 

    public void setSelection(String[] selection) { 
     for (int i = 0; i < mSelection.length; i++) { 
      mSelection[i] = false; 
      mSelectionAtStart[i] = false; 
     } 
     for (String cell : selection) { 
      for (int j = 0; j < _items.length; ++j) { 
       if (_items[j].equals(cell)) { 
        mSelection[j] = true; 
        mSelectionAtStart[j] = true; 
       } 
      } 
     } 
     simple_adapter.clear(); 
     simple_adapter.add(buildSelectedItemString()); 
    } 

    public void setSelection(List<String> selection) { 
     for (int i = 0; i < mSelection.length; i++) { 
      mSelection[i] = false; 
      mSelectionAtStart[i] = false; 
     } 
     for (String sel : selection) { 
      for (int j = 0; j < _items.length; ++j) { 
       if (_items[j].equals(sel)) { 
        mSelection[j] = true; 
        mSelectionAtStart[j] = true; 
       } 
      } 
     } 
     simple_adapter.clear(); 
     simple_adapter.add(buildSelectedItemString()); 
    } 

    public void setSelection(int index) { 
     for (int i = 0; i < mSelection.length; i++) { 
      mSelection[i] = false; 
      mSelectionAtStart[i] = false; 
     } 
     if (index >= 0 && index < mSelection.length) { 
      mSelection[index] = true; 
      mSelectionAtStart[index] = true; 
     } else { 
      throw new IllegalArgumentException("Index " + index 
        + " is out of bounds."); 
     } 
     simple_adapter.clear(); 
     simple_adapter.add(buildSelectedItemString()); 
    } 

    public void setDiselection(int index) { 

     if (index >= 0 && index < mSelection.length) { 
      mSelection[index] = false; 
      mSelectionAtStart[index] = false; 

     } else { 
      throw new IllegalArgumentException("Index " + index 
        + " is out of bounds."); 
     } 
     simple_adapter.clear(); 
     simple_adapter.notifyDataSetChanged(); 
     simple_adapter.add(buildSelectedItemString()); 
    } 

    public void setSelection(int[] selectedIndices) { 
     for (int i = 0; i < mSelection.length; i++) { 
      mSelection[i] = false; 
      mSelectionAtStart[i] = false; 
     } 
     for (int index : selectedIndices) { 
      if (index >= 0 && index < mSelection.length) { 
       mSelection[index] = true; 
       mSelectionAtStart[index] = true; 
      } else { 
       throw new IllegalArgumentException("Index " + index 
         + " is out of bounds."); 
      } 
     } 
     simple_adapter.clear(); 
     simple_adapter.add(buildSelectedItemString()); 
    } 

    public List<String> getSelectedStrings() { 
     List<String> selection = new LinkedList<>(); 
     for (int i = 0; i < _items.length; ++i) { 
      if (mSelection[i]) { 
       selection.add(_items[i]); 
      } 
     } 
     return selection; 
    } 

    public List<Integer> getSelectedIndices() { 
     List<Integer> selection = new LinkedList<>(); 
     for (int i = 0; i < _items.length; ++i) { 
      if (mSelection[i]) { 
       selection.add(i); 
      } 
     } 
     return selection; 
    } 

    private String buildSelectedItemString() { 
     StringBuilder sb = new StringBuilder(); 
     boolean foundOne = false; 

     for (int i = 0; i < _items.length; ++i) { 
      if (mSelection[i]) { 
       if (foundOne) { 
        sb.append(", "); 
       } 
       foundOne = true; 

       sb.append(_items[i]); 
      } 
     } 
     return sb.toString(); 
    } 

    public String getSelectedItemsAsString() { 
     StringBuilder sb = new StringBuilder(); 
     boolean foundOne = false; 

     for (int i = 0; i < _items.length; ++i) { 
      if (mSelection[i]) { 
       if (foundOne) { 
        sb.append(", "); 
       } 
       foundOne = true; 
       sb.append(_items[i]); 
      } 
     } 
     return sb.toString(); 
    } 
} 

Répondre

5

Essayez ceci. J'ai modifié votre void public setDeselection (int index) et les méthodes public boolean performClick().

public class MultiSelectSpinner1 extends Spinner implements 
    DialogInterface.OnMultiChoiceClickListener { 

    private static final String TAG = MultiSelectSpinner1.class.getSimpleName(); 
    String[] _items = null; 
    boolean[] mSelection = null; 
    boolean[] mSelectionAtStart = null; 
    String _itemsAtStart = null; 

    ArrayAdapter<String> simple_adapter; 
    private OnMultipleItemsSelectedListener listener; 

    AlertDialog alertDialog; 

    public MultiSelectSpinner1(Context context) { 
     super(context); 
     simple_adapter = new ArrayAdapter<>(context, 
      android.R.layout.simple_spinner_item); 
     super.setAdapter(simple_adapter); 
    } 

    public MultiSelectSpinner1(Context context, AttributeSet attrs) { 
     super(context, attrs); 

     simple_adapter = new ArrayAdapter<>(context, 
      android.R.layout.simple_spinner_item); 
     super.setAdapter(simple_adapter); 
    } 

    public void setListener(OnMultipleItemsSelectedListener listener) 
    { 
     this.listener = listener; 
    } 

    public void onClick(DialogInterface dialog, int which, boolean isChecked) { 
     if (mSelection != null && which < mSelection.length) { 
      Log.e("_items.length", "_items.length" + _items.length); 
      Log.e("_itemsAtStart", "_itemsAtStart" + _itemsAtStart); 


      if (getSelectedIndices().size() < 3) { 
       mSelection[which] = isChecked; 
       simple_adapter.clear(); 
       simple_adapter.add(buildSelectedItemString()); 
      } else { 
       Toast.makeText(getContext(),"Exceeds",Toast.LENGTH_SHORT).show(); 
       setDeselection(which); 

      } 
     } else { 
      throw new IllegalArgumentException(
       "Argument 'which' is out of bounds."); 
     } 
    } 

    @Override 
    public boolean performClick() { 
     buildDialogue(); 
     return true; 
    } 

    private void buildDialogue() { 
     if(alertDialog!=null&&alertDialog.isShowing()) 
      alertDialog.dismiss(); 

     AlertDialog.Builder builder = new  AlertDialog.Builder(getContext()); 
     builder.setTitle("Please select!!!"); 
     builder.setMultiChoiceItems(_items, mSelection, this); 
     _itemsAtStart = getSelectedItemsAsString(); 
     builder.setPositiveButton("Submit", new DialogInterface.OnClickListener() { 
      @Override 
      public void onClick(DialogInterface dialog, int which) { 
       System.arraycopy(mSelection, 0, mSelectionAtStart, 0, mSelection.length); 
       if (listener != null) { 
        listener.selectedIndices(getSelectedIndices()); 
        listener.selectedStrings(getSelectedStrings()); 
       } 
      } 
     }); 
     builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { 
      @Override 
      public void onClick(DialogInterface dialog, int which) { 
       simple_adapter.clear(); 
       simple_adapter.add(_itemsAtStart); 
       System.arraycopy(mSelectionAtStart, 0, mSelection, 0, mSelectionAtStart.length); 
      } 
     }); 
     alertDialog = builder.create(); 
     alertDialog.show(); 
    } 

    @Override 
    public void setAdapter(SpinnerAdapter adapter) { 
     throw new RuntimeException(
      "setAdapter is not supported by MultiSelectSpinner."); 
    } 

    public void setItems(String[] items) { 
     _items = items; 
     mSelection = new boolean[_items.length]; 
     mSelectionAtStart = new boolean[_items.length]; 
     simple_adapter.clear(); 
     simple_adapter.add(_items[0]); 
     Arrays.fill(mSelection, false); 
     mSelection[0] = true; 
     mSelectionAtStart[0] = true; 
    } 

    public void setItems(List<String> items) { 
     _items = items.toArray(new String[items.size()]); 
     mSelection = new boolean[_items.length]; 
     mSelectionAtStart = new boolean[_items.length]; 
     simple_adapter.clear(); 
     simple_adapter.add(_items[0]); 
     Arrays.fill(mSelection, false); 
     mSelection[0] = true; 
    } 

    public void setSelection(String[] selection) { 
     for (int i = 0; i < mSelection.length; i++) { 
      mSelection[i] = false; 
      mSelectionAtStart[i] = false; 
     } 
     for (String cell : selection) { 
      for (int j = 0; j < _items.length; ++j) { 
       if (_items[j].equals(cell)) { 
        mSelection[j] = true; 
        mSelectionAtStart[j] = true; 
       } 
      } 
     } 
     simple_adapter.clear(); 
     simple_adapter.add(buildSelectedItemString()); 
    } 

    public void setSelection(List<String> selection) { 
     for (int i = 0; i < mSelection.length; i++) { 
      mSelection[i] = false; 
      mSelectionAtStart[i] = false; 
     } 
     for (String sel : selection) { 
      for (int j = 0; j < _items.length; ++j) { 
       if (_items[j].equals(sel)) { 
        mSelection[j] = true; 
        mSelectionAtStart[j] = true; 
       } 
      } 
     } 
     simple_adapter.clear(); 
     simple_adapter.add(buildSelectedItemString()); 
    } 

    public void setSelection(int index) { 
     for (int i = 0; i < mSelection.length; i++) { 
      mSelection[i] = false; 
      mSelectionAtStart[i] = false; 
     } 
     if (index >= 0 && index < mSelection.length) { 
      mSelection[index] = true; 
      mSelectionAtStart[index] = true; 
     } else { 
      throw new IllegalArgumentException("Index " + index 
       + " is out of bounds."); 
     } 
     simple_adapter.clear(); 
     simple_adapter.add(buildSelectedItemString()); 
    } 

    public void setDeselection(int index) { 
     Log.d(TAG, "setDeselection() called with: " + "index = [" + index + "]"); 

     if (index >= 0 && index < mSelection.length) { 
      mSelection[index] = false; 
      mSelectionAtStart[index] = false; 

     } else { 
      throw new IllegalArgumentException("Index " + index 
       + " is out of bounds."); 
     } 
     simple_adapter.clear(); 
     simple_adapter.notifyDataSetChanged(); 
     simple_adapter.add(buildSelectedItemString()); 

     buildDialogue(); 
    } 

    public void setSelection(int[] selectedIndices) { 
     for (int i = 0; i < mSelection.length; i++) { 
      mSelection[i] = false; 
      mSelectionAtStart[i] = false; 
     } 
     for (int index : selectedIndices) { 
      if (index >= 0 && index < mSelection.length) { 
       mSelection[index] = true; 
       mSelectionAtStart[index] = true; 
      } else { 
       throw new IllegalArgumentException("Index " + index 
        + " is out of bounds."); 
      } 
     } 
     simple_adapter.clear(); 
     simple_adapter.add(buildSelectedItemString()); 
    } 

    public List<String> getSelectedStrings() { 
     List<String> selection = new LinkedList<>(); 
     for (int i = 0; i < _items.length; ++i) { 
      if (mSelection[i]) { 
       selection.add(_items[i]); 
      } 
     } 
     return selection; 
    } 

    public List<Integer> getSelectedIndices() { 
     List<Integer> selection = new LinkedList<>(); 
     for (int i = 0; i < _items.length; ++i) { 
      if (mSelection[i]) { 
       selection.add(i); 
      } 
     } 
     return selection; 
    } 

    private String buildSelectedItemString() { 
     StringBuilder sb = new StringBuilder(); 
     boolean foundOne = false; 

     for (int i = 0; i < _items.length; ++i) { 
      if (mSelection[i]) { 
       if (foundOne) { 
        sb.append(", "); 
       } 
       foundOne = true; 

       sb.append(_items[i]); 
      } 
     } 
     return sb.toString(); 
    } 

    public String getSelectedItemsAsString() { 
     StringBuilder sb = new StringBuilder(); 
     boolean foundOne = false; 

     for (int i = 0; i < _items.length; ++i) { 
      if (mSelection[i]) { 
       if (foundOne) { 
        sb.append(", "); 
       } 
       foundOne = true; 
       sb.append(_items[i]); 
      } 
     } 
     return sb.toString(); 
    } 

    public interface OnMultipleItemsSelectedListener { 
     void selectedIndices(List<Integer> indices); 

     void selectedStrings(List<String> strings); 
    } 
} 
+0

c'est très bien. J'ai un problème . J'ai la disposition qui a 2 à 3 fileur de sélection multiple. J'ai un bouton de soumission aussi. Le doute est de savoir comment puis-je obtenir les valeurs sélectionnées pour chaque spinner. Et je dois sauvegarder ces valeurs. Donc, puis-je obtenir les valeurs sélectionnées. Et comment puis-je savoir qui est l'utilisateur sélectionné, je reçois la valeur sélectionnée sur public void selectedStrings (Liste chaînes), mais comment je peux différencier quelles valeurs sont pour mon 1er spinner et pour 2e spinner et 3e spinner . Avez-vous une idée..? –

+0

Travailler comme charme grâce .. –