20

我一直在绞尽脑汁试图让它发挥作用。我想从 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"/>

谢谢你的帮助。

4

3 回答 3

52

你很亲密。尝试这个:

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);
于 2013-04-10T00:20:11.173 回答
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);
于 2015-03-18T02:25:04.423 回答
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 的值更改为要在当前年份之前打印多少年。

于 2021-07-27T14:53:20.760 回答