How to use Android Spinner with multiple choice in Fragment of Navigation drawer activity
如何创建允许选择多个项目的微调器,即带有复选框的微调器?同时我需要在导航抽屉活动的片段中使用这个多选微调器。
任何人都可以用合适的示例代码解决我的疑问。
提前谢谢你!!!
public Doctor() {
// Required empty public constructor
}
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 | @Override public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) { // Inflate the layout for this fragment getActivity().setTitle("Doctor"); View v = inflater.inflate(R.layout.fragment_doctor, container, false); String [] values = {"All Town","Paris","Kodambakkam","Nungambakkam","T.Nagar","Egmore"}; Spinner spinner = (Spinner) v.findViewById(R.id.town); ArrayAdapter<String> adapter = new ArrayAdapter<String>(this.getActivity(), android.R.layout.simple_spinner_item, values); adapter.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line); spinner.setAdapter(adapter); String [] values1 = {"Select Doctor","Doctor1","Doctor2","Doctor3","Doctor4"}; Spinner spinner1 = (Spinner) v.findViewById(R.id.doctor); ArrayAdapter<String> adapter1 = new ArrayAdapter<String>(this.getActivity(), android.R.layout.simple_spinner_item, values1); adapter.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line); spinner1.setAdapter(adapter1); String [] values2 = {"Worked With","Nagaraj","Muthuvel"}; Spinner spinner2 = (Spinner) v.findViewById(R.id.doctor); ArrayAdapter<String> adapter2 = new ArrayAdapter<String>(this.getActivity(), android.R.layout.simple_spinner_item, values2); adapter.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line); spinner2.setAdapter(adapter2); return v; } public void onViewCreated(View view,@Nullable Bundle savedInstanceState){ super.onViewCreated(view, savedInstanceState); } |
}
我需要将 spinner2 更改为我们的多项选择微调器。我正在从 Fragment 扩展课程。所以不知道如何处理这个多选微调器
1 How do I create spinner which allows to choose multiple items, i.e spinner with check boxes?
尝试使用 multiselectionspinner。
https://stackoverflow.com/a/6022474/6616489
对于
multiple choice spinner inside the fragment of Navigation drawer
参考 https://stackoverflow.com/a/20017862/6616489
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 | public class Doctor extends Fragment{ MultiSelectionSpinner spinner2; FragmentManager fm = getFragmentManager(); public Doctor() { // Required empty public constructor } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) { // Inflate the layout for this fragment getActivity().setTitle("Doctor"); View v = inflater.inflate(R.layout.fragment_doctor, container, false); String [] values = {"All Town","Paris","Kodambakkam","Nungambakkam","T.Nagar","Egmore"}; Spinner spinner = (Spinner) v.findViewById(R.id.town); ArrayAdapter<String> adapter = new ArrayAdapter<String>(this.getActivity(), android.R.layout.simple_spinner_item, values); adapter.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line); spinner.setAdapter(adapter); String [] values1 = {"Select Doctor","Doctor1","Doctor2","Doctor3","Doctor4"}; Spinner spinner1 = (Spinner) v.findViewById(R.id.se_doctor); ArrayAdapter<String> adapter1 = new ArrayAdapter<String>(this.getActivity(), android.R.layout.simple_spinner_item, values1); adapter1.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line); spinner1.setAdapter(adapter1); String [] values2 = {"Worked With","Nagaraj","Muthuvel"}; // Multi spinner spinner2 = (MultiSelectionSpinner)v.findViewById(R.id.work); spinner2.setItems(values2);return v; }} |
上面的代码是多选微调器(注:微调器2)
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 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 | public class MultiSelectionSpinner extends Spinner implements OnMultiChoiceClickListener { String[] _items = null; boolean[] mSelection = null; ArrayAdapter<String> simple_adapter; public MultiSelectionSpinner(Context context) { super(context); simple_adapter = new ArrayAdapter<String>(context, android.R.layout.simple_spinner_item); super.setAdapter(simple_adapter); } public MultiSelectionSpinner(Context context, AttributeSet attrs) { super(context, attrs); simple_adapter = new ArrayAdapter<String>(context, android.R.layout.simple_spinner_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(); simple_adapter.add(buildSelectedItemString()); } else { throw new IllegalArgumentException( "Argument 'which' is out of bounds."); } } @Override public boolean performClick() { AlertDialog.Builder builder = new AlertDialog.Builder(getContext()); builder.setMultiChoiceItems(_items, mSelection, this); 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(_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()); } 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]); } } 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(); }} |
以上代码用于创建从 spinner2 调用的多选微调器。
请检查代码。如果有任何疑问,请在此处发表评论,让我尝试消除您的疑问。