【问题标题】:How to set existing data to the Multiselection Spinner in android如何在android中将现有数据设置为Multiselection Spinner
【发布时间】:2021-04-08 12:26:40
【问题描述】:

您好,在下面的代码中,我实现了多选微调器工作正常。现在我正在渲染一个 api 来获取来自服务器的药物名称。

示例: 下面的代码包含用逗号分隔的字符串列表。

从服务器我得到字符串列表中的字符串,来自服务器的数据是什么想要设置字符串

谁能帮忙

完整列表:abc,cde,jkl,efg 用逗号分隔 药物名称适配器.java:

public class MedicationnamesAdapter extends AppCompatSpinner implements DialogInterface.OnMultiChoiceClickListener {


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


    int position;
    String[] _items = null;
    boolean[] mSelection = null;
    ArrayAdapter<String> simple_adapter;
    private int sbLength;
    String _itemsAtStart = null;
    boolean[] mSelectionAtStart = null;

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

    public MedicationnamesAdapter(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 setListener(MedicationnamesAdapter.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();
            if (buildSelectedItemString().length() > 0) {
                simple_adapter.add(buildSelectedItemString());
            }  else {
                simple_adapter.add("Medication Name");
            }
        } else {
            throw new IllegalArgumentException(
                    "Argument 'which' is out of bounds");
        }
    }

    @Override
    public boolean performClick() {
        final 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 arg0, int arg1) {
                System.arraycopy(mSelection, 0, mSelectionAtStart, 0, mSelection.length);
                listener.selectedIndices(getSelectedIndices());
                listener.selectedMEdicationNamesStrings(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);
            }
        });
        /*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];
        mSelectionAtStart  = new boolean[_items.length];
        simple_adapter.clear();


        simple_adapter.add("Medication Name");

        ///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> 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]);
            }
        }

        //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]);
            }
        }
        /*String sbCheck;
        if (sb.length()>0){
           sbCheck=sb.toString();
        }else{
            sbCheck="Tap to select";
        }*/
        return sb.toString();
    }

    private boolean isAnySelect() {
        for (boolean b : mSelection) {
            if (b == true) return true;
        }
        return false;
    }
}

MainActivity.java:

我正在从 Api 传输数据

String drug_name=medical_medications1.getMedicationName();

输出:abc

如何将 abc 设置为上述 MedicationnamesAdapter

【问题讨论】:

    标签: android multi-select


    【解决方案1】:

    您可以在适配器类中创建一个函数

    类似的,

    public void addItem(String medication) {
       // add to your array here
       notifyItemInserted(*/ position at which item is inserted in the array */)
    }
    

    从您从 API 获取药物名称的活动/片段中调用此方法。

    notifyItemInserted 用于告诉已经存在的列表列表数据的变化。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-07-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多