我正在开发一个需要Android多选微调器项目的应用。我能够正确获得微调器并选择多个项目并用逗号分隔,但我的问题是,当我选择一个项目时,我需要获取该项目的值,并且我需要在微调器中显示项目名称,但将值作为该项目发送值: 例如,我有动物列表,例如:Lion,King,Elephant..etc这些值,例如LN,KN,ELP..etc ..当我选择Lion,King ...时,我能够成功显示逗号(,)分隔旋转器上的名称,但我需要将值发送为LN,KN ..
这是我的多选微调器代码:
public class MultiSelectionSpinner 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 MultiSelectionSpinner(Context context) {
super(context);
simple_adapter = new ArrayAdapter<>(context,
android.R.layout.simple_spinner_item);
super.setAdapter(simple_adapter);
}
public MultiSelectionSpinner(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) {
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.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 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();
}
}
请在选择项目名称时更新如何获取项目值的代码。谢谢大家
答案 0 :(得分:0)
通过此链接https://github.com/GunaseelanArumaikkannu/MultiSpinner/tree/master/libmultispinner
添加库项目在应用gradle teachers = Teacher.objects.prefetch_related('class_set')
# what you want is not a valid Python structure (set of lists (looking like dicts))
# list of dicts makes more sense
result = [
{'teacher': t.pk, 'classes': t.class_set.all()}
for t in teachers
]
内编译您的图书馆项目
您的活动实现了此监听器compile project(':libmultispinner')
MultiSelectionSpinner.OnMultipleItemsSelectedListener