简体   繁体   中英

get selected strings from Multiselection spinner in android

Hi in the below code I was implemented multiselection spiiner.if user click on the multiselection spiiner it will opens a dialog box with checkboxes and button.If user selected multiple checkboxes and then click on ok button it will spearted with a comma and then set to spinner.

Now My problem is In selecting more textboxes how to get selected strings.I am not able to getslected strings.

public class MultipleSelectionSpinner extends AppCompatSpinner implements
    DialogInterface.OnMultiChoiceClickListener {



  String[] _items = null;
  boolean[] mSelection = null;
  ArrayAdapter<String> simple_adapter,Allegey_adapter;
  private int sbLength;

  public MultipleSelectionSpinner(Context context) {
    super(context);


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

  }

  public MultipleSelectionSpinner(Context context, AttributeSet attrs) {
    super(context, attrs);
    simple_adapter = new ArrayAdapter<String>(context,
        android.R.layout.simple_spinner_dropdown_item);
    super.setAdapter(simple_adapter);

  }



  public void onClick(DialogInterface dialog, int which, boolean isChecked) {
    if (mSelection != null && which < mSelection.length) {
      mSelection[which] = isChecked;
      simple_adapter.clear();
//      Allegey_adapter.clear();
      if (buildSelectedItemString().length() > 0) {

        simple_adapter.add(buildSelectedItemString());
      }  else {
        simple_adapter.add("Pre-Existing Condition");

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

  @Override
  public boolean performClick() {
    final AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
    builder.setMultiChoiceItems(_items, mSelection, this);

    builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
      @Override
      public void onClick(DialogInterface arg0, int arg1) {

      }

    });
    builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
      @Override
      public void onClick(DialogInterface dialog, int which) {

      }
    });
        /*if (mSelection.length > 3){
            Toast.makeText(getContext(), "Cannot select more than 3", Toast.LENGTH_SHORT).show();
            return false;
        }*/
    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];
    simple_adapter.clear();
    simple_adapter.add(_items[0]);
    Arrays.fill(mSelection, false);
  }

  public void setItems(List<String> items) {
    _items = items.toArray(new String[items.size()]);
    mSelection = new boolean[_items.length];
    simple_adapter.clear();

      simple_adapter.add("Pre-Existing Condition");

    ///simple_adapter.add(_items[0]);
    Arrays.fill(mSelection, false);
  }

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

  public void setSelection(List<String> selection) {
    for (int i = 0; i < mSelection.length; i++) {
      mSelection[i] = false;
    }
    for (String sel : selection) {
      for (int j = 0; j < _items.length; ++j) {
        if (_items[j].equals(sel)) {
          mSelection[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;
    }
    if (index >= 0 && index < mSelection.length) {
      mSelection[index] = true;
    } else {
      throw new IllegalArgumentException("Index " + index
          + " is out of bounds.");
    }
    simple_adapter.clear();
    simple_adapter.add(buildSelectedItemString());
        /*if (sbLength>0){
            Toast.makeText(getContext(), "Length greater than zero", Toast.LENGTH_SHORT).show();
            simple_adapter.add(buildSelectedItemString());
        }else{
            Toast.makeText(getContext(), "Length shorter", Toast.LENGTH_SHORT).show();
            simple_adapter.add("Tap to select");
        }*/
  }

  public void setSelection(int[] selectedIndicies) {
    for (int i = 0; i < mSelection.length; i++) {
      mSelection[i] = false;
    }
    for (int index : selectedIndicies) {
      if (index >= 0 && index < mSelection.length) {
        mSelection[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<String>();
    for (int i = 0; i < _items.length; ++i) {
      if (mSelection[i]) {
        selection.add(_items[i]);
      }
    }
    return selection;
  }

  public List<Integer> getSelectedIndicies() {
    List<Integer> selection = new LinkedList<Integer>();
    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]);
      }
    }

    //Log.e("sb length",""+sb.length());
    sbLength = sb.length();
    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();
  }
}

java:

pre_existing_condition.setItems(Pre_Conditions);

Have an ArrayList initialized and in the checkbox's click listeners, add the string to the array like below:

    final ArrayList<String> myArrayList = new ArrayList<>();
    checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton myCheckBox, boolean checked) {
        if (checked) {
            myArrayList.add(myCheckBox.getText().toString());
        } else {
            myArrayList.remove(myCheckBox.getText().toString());
        }
        }
    });

then when the user clicks the button you want, you can loop though the populated array and make a comma separated string like so:

    String myString = "";
    for (int i = 0; i < myArrayList.size(); i++) {
        myString+= myArrayList.get(i) + ",";
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM