Populate spinner with years dynamically in Android?

前端 未结 2 1272
广开言路
广开言路 2021-02-05 07:30

I have been racking my brain trying to get this to work. I want to dynamically enter in years from 1900 to the current year into a spinner. I don\'t think that this is possible

2条回答
  •  生来不讨喜
    2021-02-05 08:03

    You're very close. Try this:

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

    You just forgot to add

    spinYear.setAdapter(adapter);
    

提交回复
热议问题