在Android中动态填充spinner多年?

时间:2013-04-10 00:06:58

标签: android dynamic insert spinner

我一直绞尽脑汁想要让它发挥作用。我希望从1900年到现在的年份动态地进入旋转器。我不认为这可以使用XML定义的数组,但我可以使用数组适配器吗?以下是我到目前为止的情况:

ArrayList<String> years = new ArrayList<String>();
int thisYear = Calendar.getInstance().get(Calendar.YEAR);

for (int i = 1900; i <= thisYear; i++) 
{
    years.add(i);
}

adapter = new ArrayAdapter<String>(this, 
           android.R.layout.simple_spinner_item, years);

//spinner to enter this list to
spinYear = (Spinner)findViewById(R.id.yearspin);

这是微调器的XML:

<Spinner
        android:id="@+id/yearspin"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"/>

感谢您的帮助。

3 个答案:

答案 0 :(得分:43)

你非常接近。试试这个:

ArrayList<String> years = new ArrayList<String>();
int thisYear = Calendar.getInstance().get(Calendar.YEAR);
for (int i = 1900; i <= thisYear; i++) {
    years.add(Integer.toString(i));
}
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, years);

Spinner spinYear = (Spinner)findViewById(R.id.yearspin);
spinYear.setAdapter(adapter);

你忘了添加

spinYear.setAdapter(adapter);

答案 1 :(得分:6)

我希望它会对你有所帮助。

static final String[] Months = new String[] { "January", "February",
            "March", "April", "May", "June", "July", "August", "September",
            "October", "November", "December" };

 // Set years
        ArrayList<String> years = new ArrayList<String>();
        int thisYear = Calendar.getInstance().get(Calendar.YEAR);
        for (int i = 1900; i <= thisYear; i++) {
            years.add(Integer.toString(i));
        }
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, years);

        Spinner spinYear = (Spinner)findViewById(R.id.spinnerYears);
        spinYear.setAdapter(adapter);
        // Set months
        ArrayAdapter<String> adapterMonths = new ArrayAdapter<String>(this,
                android.R.layout.simple_spinner_item, Months);
        adapterMonths.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

        Spinner spinMonths = (Spinner)findViewById(R.id.spinnerMonths);
        spinMonths.setAdapter(adapterMonths);
        // Set days
        ArrayList<String> days = new ArrayList<String>();
        for (int i = 1; i <= 31; i++) {
            days.add(Integer.toString(i));
        }
        ArrayAdapter<String> adapterDays = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, days);

        Spinner spinDays = (Spinner)findViewById(R.id.spinnerDays);
        spinDays.setAdapter(adapterDays);

答案 2 :(得分:0)

我建议简单的:

ArrayList<String> yearsList = new ArrayList<>();
int current_year = Calendar.getInstance().get(Calendar.YEAR);
int count = 60;
for (int i = 0; i < count; i++) {
    yearsList.add(Integer.toString(current_year - i));
}

ArrayAdapter<String> yearsAdapter = new ArrayAdapter<>(getContext(), R.layout.dropdown_item, yearsList);
spinYear.setAdapter(yearsAdapter);

只需将 int count 的值更改为您想在当前年份之前打印多少年。