In my app, I am using NumberPicker
with values in Hebrew and it looks like this:
The values are taken from an array like this:
String times[] = {"ינו'-18", "פבר'-18", "מרץ-18", "אפר'-18", "מאי-18", "יוני-18", "יולי-18"
, "אוג'-18", "ספט'-18", "אוק'-18", "נוב'-18", "דצמ'-18", "ינו'-19", "פבר'-19", "מרץ-19", "אפר'-19", "מאי-19", "יוני-19", "יולי-19"
, "אוג'-19", "ספט'-19", "אוק'-19", "נוב'-19",};
The Hebrew values in the array correspond to the months like this :
ינו' is The first month in the year (January)
פבר' is the second month(February)
מרץ' is month 3 (march)
אפר is month 4 (April)
מאי is month 5 (May)
יוני is month 6 (June)
יולי is month 7 (July)
אוג is month 8 (August)
ספט is month 9 (september)
אוק is month 10 (october)
נוב is month 11 (November)
דצמ is month 12 (December)
I want to set my array values so it will start from the current month (for example if we are on June-2021 the first value will be יוני-2021) and will include all of the months in the next year as well.
At first, I thought of saving the array in remote database/locally with a lot of values but I realized that this is just a bad solution Because I will need to change the values in the array constantly.
How can I know what values (combining both Hebrew and the year like above) do I need to put in the array according to the current month and year?
Here is some extra information - here is the method that I am using to set the numberpicker
values:
private void setNumberPicker(NumberPicker nubmerPicker, String[] numbers) {
nubmerPicker.setMaxValue(numbers.length - 1);
nubmerPicker.setMinValue(0);
nubmerPicker.setWrapSelectorWheel(true);
nubmerPicker.setDisplayedValues(numbers);
}
In the end, I found a solution using a custom class that I made:
public class YearAndMontsForNumberPicker {
private String[] demoArray = {"ינו'-xx", "פבר'-xx", "מרץ-xx", "אפר'-xx", "מאי-xx", "יוני-xx", "יולי-xx"
, "אוג'-xx", "ספט'-xx", "אוק'-xx", "נוב'-xx", "דצמ'-xx" , "ינו'-xx", "פבר'-xx", "מרץ-xx", "אפר'-xx", "מאי-xx", "יוני-xx", "יולי-xx"
, "אוג'-xx", "ספט'-xx", "אוק'-xx", "נוב'-xx", "דצמ'-xx"};
private Calendar c = Calendar.getInstance(); //get the proper replacements for the demo array
private int currentMonth = c.get(Calendar.MONTH);
private String currentYear = (c.get(Calendar.YEAR) + "").substring(2);
private String[] finalArray;
//sets the array that will be later use with numberPicker
public String[] setArrayForCurrentYear() {
finalArray = Arrays.copyOfRange(demoArray, currentMonth, demoArray.length);
changeArrayxx(finalArray,currentYear,0,12-currentMonth); //replace the first part with current year
changeArrayxx(finalArray, (Integer.valueOf(currentYear)+1)+"",12-currentMonth,finalArray.length); //replace the end of the array with the next year
return finalArray;
}
private void changeArrayxx(String [] array,String currentyear,int iterator,int condition){
for (int i = iterator; i < condition; i++) { // chang the xx in the array with the last 2 digits
array[i] = array[i].replace("xx", currentyear);
}
}
}
Now all if left to do get Instance of this class like this:
new YearAndMontsForNumberPicker().setArrayForCurrentYear()
For my case, I called this inside setNumberPicker
method (check the question):
setNubmerPicker(currentYearNumberPicker, new YearAndMontsForNumberPicker().setArrayForCurrentYear());