C
C
Chvalov2015-10-16 15:37:11
Java
Chvalov, 2015-10-16 15:37:11

Dependencies between two spinners and transfer of position to the second spinner, how to implement?

There are two spinners:

String[] outputChannel = {"1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12"};
    String[] sensorType = {"1", "2", "3", "4"};

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_data_input, container, false);
        // адаптер спинера - Выходные каналы 1-12
        ArrayAdapter<String> AdapterOutputChannel = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_spinner_item, outputChannel);
        AdapterOutputChannel.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

        final Spinner SpinnerOutputChanel = (Spinner) view.findViewById(R.id.OutputChannelSpinner);
        SpinnerOutputChanel.setAdapter(AdapterOutputChannel);
        // устанавливаем обработчик нажатия
        SpinnerOutputChanel.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                Toast.makeText(getActivity(), "Position = " + position, Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onNothingSelected(AdapterView<?> arg0) {
            }
        });

        // адаптер спинера - Тип датчика 1-4
        ArrayAdapter<String> AdapterSensorType = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_spinner_item, sensorType);
        AdapterSensorType.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

        Spinner SpinnerSensorType = (Spinner) view.findViewById(R.id.SensorTypeSpinner);
        SpinnerSensorType.setAdapter(AdapterSensorType);
        // устанавливаем обработчик нажатия
        SpinnerSensorType.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                Toast.makeText(getActivity(), "Position = " + position, Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onNothingSelected(AdapterView<?> arg0) {
            }
        });

        return view;
    }

And there is an array of bytes: [1, 3, 1, 2, 2, 2, 1, 1, 4, 3, 3, 3]in which the value is from 1 to 4.
And you need to implement the following : When choosing a value from 1 to 12 in the first spinner, look at the selected value in the array and transfer the digit with the position from it to the next spinner.
For example, in the first spinner I chose 7, then in the array I look at the 7 value, which is exactly = 1
and pass it as the position for the second spinner.
Here I found some examples, but they are not what I need: ticks 1 and ticks 2

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question