Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I need a dynamic string-array. All the examples I have seen show hard-coded string-arrays. Basically I have a list of years for the user to select. The years are hard coded, but unfortunately current year will not be a relevant choice next year. I don't want to push updates for this in the future this could be dynamically generated with some kind of system call of the current year + X amount of years in the future for the rest of the list.

I am not sure how to make variables edit the XML, any insight appreciated

Clarification: I have edited the Res>Values>strings.xml to have a list of years. I manually typed years in the list-item,

it would be: list-item 2011 list-item 2012 list-item 2013..........

but in the year 2012, older years will not be relevant so I would need this string array regenerated. Is there a way to make the typed years be variables?

such as it would be: list-item YEAR list-item YEAR+1 list-item YEAR+2

and a function in the java will let the list know what those values really are, or something

share|improve this question
 
What do you mean by "hardcoded"? In a java class or in a res xml? –  Aleadam Apr 3 '11 at 3:14
1  
@RD.: It's not quite clear what you want to do. You can always get the 'current' year dynamically using one of the date/time classes (Gregorian Calendar, Calendar, Date etc). –  Squonk Apr 3 '11 at 3:41
add comment

2 Answers

up vote 1 down vote accepted

I'm not sure what the problem is. Perhaps you're not sure how to do in Java something that you were previously doing in XML?

Creating an ArrayList of years is simple. This can back a ListView or a Spinner by using an ArrayAdapter.

ArrayList<Integer> years = new ArrayList<Integer>();
int thisYear = Calendar.getInstance().get(Calendar.YEAR);
for (int i = 1990; i <= thisYear; i++) {
    years.add(i);
}
share|improve this answer
 
edited my original post for clarification, this may be the solution I will look further into it –  CQM Apr 4 '11 at 0:01
 
Thanks. I had to ask it twice but I got it now. Was simple, just was thinking about it wrong. –  CQM Apr 9 '11 at 22:55
add comment

Once you compile you apk and it's signed, it's sealed. There is no way to edit XML at that point. Your answer will be to use one of the many java date functions that retrieves the year(s) based on already understood calendar dates.

If you have an example post it here

share|improve this answer
 
I've edited my original post, but I may have a solution here too –  CQM Apr 4 '11 at 0:01
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.