Multiselection微调器OnItemSelectedListener在android中不工作

Multiselection微调器OnItemSelectedListener在android中不工作,android,multipleselection,Android,Multipleselection,您好,在下面的代码中,我正在尝试在选中复选框时实现Multiselection微调器。单击后,它将设置为Multiselection微调器。在获取所选项目时,它将不起作用 有人能帮我吗 Multiselection spinner.onclick: allergies.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { @Override

您好,在下面的代码中,我正在尝试在选中复选框时实现Multiselection微调器。单击后,它将设置为Multiselection微调器。在获取所选项目时,它将不起作用

有人能帮我吗

Multiselection spinner.onclick:

allergies.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                            @Override
                            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                                selected = allergies.getSelectedStrings();
                                Log.d("selected", String.valueOf(selected));
                            }
                            @Override
                            public void onNothingSelected(AdapterView<?> parent) {
                            }
                        });
allergies.setOnItemSelectedListener(新的AdapterView.OnItemSelectedListener(){
@凌驾
已选择公共视图(AdapterView父视图、视图视图、整型位置、长id){
selected=过敏。getSelectedStrings();
Log.d(“选定”,String.valueOf(选定));
}
@凌驾
未选择公共无效(AdapterView父级){
}
});
MultipleSelectionSpinner.java:

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();
  }

  private boolean isAnySelect() {
    for (boolean b : mSelection) {
      if (b == true) return true;
    }
    return false;
  }
}
public类MultipleSelectionSpinner扩展了AppCompatSpinner实现
DialogInterface.OnMultiChoiceClickListener{
字符串[]_items=null;
布尔[]mSelection=null;
ArrayAdapter simple_适配器、Allegey_适配器;
私人长度;
公共多重选择微调器(上下文){
超级(上下文);
简单_适配器=新的阵列适配器(上下文,
android.R.layout.simple\u微调器\u下拉菜单\u项);
super.setAdapter(简单_适配器);
}
公共多重选择微调器(上下文上下文、属性集属性){
超级(上下文,attrs);
简单_适配器=新的阵列适配器(上下文,
android.R.layout.simple\u微调器\u下拉菜单\u项);
super.setAdapter(简单_适配器);
}
public void onClick(DialogInterface dialog,int,其中布尔值被选中){
if(mSelection!=null&&which0){
简单的适配器。添加(buildSelectedItemString());
}否则{
简单适配器。添加(“预先存在的条件”);
}
}否则{
抛出新的IllegalArgumentException(
“超出范围的参数”);
}
}
@凌驾
公共布尔performClick(){
final AlertDialog.Builder=新建AlertDialog.Builder(getContext());
builder.setMultiChoiceItems(_items,mSelection,this);
setPositiveButton(“确定”,新的DialogInterface.OnClickListener(){
@凌驾
公共void onClick(对话框接口arg0,int arg1){
}
});
setNegativeButton(“取消”,新建DialogInterface.OnClickListener()){
@凌驾
public void onClick(DialogInterface dialog,int which){
}
});
/*如果(mSelection.length>3){
Toast.makeText(getContext(),“不能选择超过3”,Toast.LENGTH_SHORT.show();
返回false;
}*/
builder.show();
返回true;
}
@凌驾
公用void setAdapter(SpinnerAdapter适配器){
抛出新的运行时异常(
“MultiSelectSpinner不支持setAdapter。”);
}
公共void集合项(字符串[]项){
_项目=项目;
mSelection=新布尔值[_items.length];
简单_适配器。清除();
简单_适配器。添加(_项[0]);
数组。填充(mSelection,false);
}
公共无效集合项(列表项){
_items=items.toArray(新字符串[items.size()]);
mSelection=新布尔值[_items.length];
简单_适配器。清除();
简单适配器。添加(“预先存在的条件”);
///简单_适配器。添加(_项[0]);
数组。填充(mSelection,false);
}
公共选择(字符串[]选择){
用于(字符串单元格:选择){
对于(int j=0;j<_items.length;++j){
如果(_items[j].equals(单元格)){
mSelection[j]=真;
}
}
}
}
公开选举(名单选择){
对于(int i=0;i=0&&index0){
Toast.makeText(getContext(),“长度大于零”,Toast.Length_SHORT.show();
简单的适配器。添加(buildSelectedItemString());
}否则{
Toast.makeText(getContext(),“Length SHORT”,Toast.Length_SHORT.show();
简单适配器。添加(“点击选择”);
}*/
}
公共选举(int[]selecteddicies){
对于(int i=0;i=0&&index