ANDROID布局包含多个MultiSelectionSpinner [英] ANDROID a layout contains multiple MultiSelectionSpinner

查看:78
本文介绍了ANDROID布局包含多个MultiSelectionSpinner的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个xml布局,其中有2到3个多选微调框.我也有一个提交按钮.

I have a xml layout which is having 2 to 3 multiple selection spinner. I have one submit button too.

然后,我的疑问是如何获取每个微调器的选定值.而且我也必须保存这些值.那么我如何获得所选的值.我怎么知道用户选择了哪个微调器,我能够在public void selectedStrings(List strings)上获取选定的值,但是我如何区分哪个值适用于我的第一个微调器,第二个微调器和第三个微调器.你有什么主意吗..?

Then my doubt is how can i get the selected values for each spinner . And the i have to save these values too . So how can i get the selected values. And how can i came to know which is spinner that the user selected , I am able to get the selected value on public void selectedStrings(List strings) but how i can differentiate which values are for my 1st spinner and for 2nd spinner and 3rd spinner. Do you have any idea..?

我的代码

    public class DashBoardOne  extends Fragment implements MultiSelectionSpinner.OnMultipleItemsSelectedListener{
         MultiSelectionSpinner multiSelectionSpinnerIndustry,multiSelectionSpinnerLocation;
         List<String> arrayIndustry,arrayLocation; 
         Button BtnSubmitAspiration;
        public DashBoardOne() {
            arrayIndustry = new ArrayList<String>();
        }

        public static DashBoardOne newInstance(String text) {
            DashBoardOne dashBoardOne = null;
             // some code
            return dashBoardOne;
        }

        @Nullable
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            View view= inflater.inflate(R.layout.fragment_dashboard, container, false);

            multiSelectionSpinnerIndustry = (MultiSelectionSpinner) view.findViewById(R.id.SpinnerIndustry);
            arrayIndustry=db.fillIndustrySpinner(); // gettting the list for first spinner from db
            arrayLocation=db.fillLocationSpinner();  // gettting the list for 2nd spinner from db 

            multiSelectionSpinnerIndustry.setItems(arrayIndustry); //first spinner
            multiSelectionSpinnerIndustry.setListener(this);

            multiSelectionSpinnerLocation.setItems(arrayLocation); // 2nd spinner 
            multiSelectionSpinnerLocation.setListener(this);

            BtnSubmitAspiration=(Button)view.findViewById(R.id.BtnSubmitAspiration);

              BtnSubmitAspiration.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    // here i have some code to save the data. How can i get the selected values for my each multiSelectionSpinner ?
                }
            });
    }


     @Override
        public void selectedIndices(List<Integer> indices) {
        }
        @Override
        public void selectedStrings(List<String> strings) {
            Toast.makeText(getActivity().ge

tApplicationContext(), 
        strings.toString(), Toast.LENGTH_LONG).show();  // here i am able to get the selected strings, 
                                                        //But i dont know how to diffrentiate the current value for which spinner 
    }
}

MultiSelectionSpinner类

class MultiSelectionSpinner

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnMultiChoiceClickListener;
import android.util.AttributeSet;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.SpinnerAdapter;

import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;


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

推荐答案

单击按钮可以为每个微调器选择项目.

You can get selected item for each spinner when button is clicked.Like this

spinner.getSelectedItem().toString();

这篇关于ANDROID布局包含多个MultiSelectionSpinner的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆