如何为动态微调器设置默认值?

时间:2017-11-29 04:27:21

标签: android android-spinner

我创造了三个旋转器,分别是年,月,日。显然,我的一天将根据我的月份改变。

这是我的功能:

private void datePick() {
        Calendar calendar = Calendar.getInstance();

        for (int i = 0; i < 100; i++) {
            dataYear.add("" + (calendar.get(Calendar.YEAR) - i));
        }

        adapterSpYear = new ArrayAdapter<String>(getActivity(), R.layout.spinner, dataYear);
        adapterSpYear.setDropDownViewResource(R.layout.support_simple_spinner_dropdown_item);
        spYear.setAdapter(adapterSpYear);

        for (int i = 1; i <= 12; i++) {
            dataMonth.add("" + (i < 10 ? "0" + i : i));
        }

        adapterSpMonth = new ArrayAdapter<String>(getActivity(), R.layout.spinner, dataMonth);
        adapterSpMonth.setDropDownViewResource(R.layout.support_simple_spinner_dropdown_item);
        spMonth.setAdapter(adapterSpMonth);

        spMonth.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int arg2, long arg3) {
                dataDay.clear();
                Calendar calendar1 = Calendar.getInstance();
                calendar1.set(Calendar.YEAR, Integer.valueOf(spYear.getSelectedItem().toString()));
                calendar1.set(Calendar.MONTH, arg2);
                int dayOfMonth = calendar1.getActualMaximum(Calendar.DAY_OF_MONTH);
                for (int i = 1; i <= dayOfMonth; i++) {
                    dataDay.add("" + (i < 10 ? "0" + i : i));
                }
                adapterSpDay = new ArrayAdapter<String>(getActivity(), R.layout.spinner, dataDay);
                adapterSpDay.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                spDay.setAdapter(adapterSpDay);
                adapterSpDay.notifyDataSetChanged();


                String year = spYear.getSelectedItem().toString();
                String month = spMonth.getSelectedItem().toString();
                String day = spDay.getSelectedItem().toString();

                Log.i("test", ".>>>>Year>>>>" + year);
                Log.i("test", ".>>>>Month>>>>" + month);
                Log.i("test", ".>>>>Day>>>>" + day);
            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {

            }
        });
    }

现在我有一个默认数据,例如1960/11/24。

我尝试找出数字位置,并使用setSelection()年份和月份,微调器将会更改。

以下是我的尝试:

Calendar calendar = Calendar.getInstance();
        int thisYear = calendar.get(Calendar.YEAR);
        int finalResult = thisYear - 1960;
        spYear.setSelection(finalResult); //year spinner will show 1960
        spMonth.setSelection(11-1); //month spinner will show 11
        spDay.setSelection(20); //day spinner will show 1

当我使用setSelection()时,我发现日期微调器无效,我猜天旋转器是基于月旋转器的动态基础。

但我不知道如何解决它?

任何帮助将不胜感激。提前谢谢。

1 个答案:

答案 0 :(得分:1)

你必须做这样的事情。调用适配器的位置

Calendar calendar = Calendar.getInstance();
        int thisYear = calendar.get(Calendar.YEAR);
        int finalResult = thisYear - 1960;
        int positionYear = adapterSpYear.getPosition(String.valueOf(finalResult))
        spYear.setSelection(positionYear); //year spinner will show 1960
        //same goes for month and day

现有answer。 这是docs