3

so i have 7 different button, i want to sethint on each button with current date++ orderly.

DateTimeFormatter dateFormater = DateTimeFormatter.ofPattern("d");
        ZoneId zone = ZoneId.of("Asia/Jakarta");
        LocalDate date = LocalDate.now(zone);
        int amount = 1;
        int buttonCount = 7;
        for (int i = 0; i < buttonCount; i++){
            hari1.setHint(date.format(dateFormater));
            date = date.plusDays(amount);
            hari2.setHint(date.format(dateFormater));
            date = date.plusDays(amount);
            hari3.setHint(date.format(dateFormater));
            date = date.plusDays(amount);
            hari4.setHint(date.format(dateFormater));
            date = date.plusDays(amount);
            hari5.setHint(date.format(dateFormater));
            date = date.plusDays(amount);
            hari6.setHint(date.format(dateFormater));
            date = date.plusDays(amount);
            hari7.setHint(date.format(dateFormater));
        }

date output start 27-28-29 ect. which is wrong becuse today date is 22. the output should be 22-23-24 ect right ? so i tried using calendar on date and the output is correct 22. why ? and is there solution so i can get the correct date and got it (date++) ? and is there another approach ? how ?

        Date today = Calendar.getInstance().getTime();
        final SimpleDateFormat fcDate = new SimpleDateFormat("dd");
forpas
  • 160,666
  • 10
  • 38
  • 76
  • Glad to see that you posted a question [about the problem we discussed earlier](https://stackoverflow.com/questions/52833276/how-do-i-put-date-orderly-on-each-button/52852334#comment105792985_52852334) and got an answer or two that seem to be helpful. – Ole V.V. Jan 22 '20 at 05:16

2 Answers2

3

The problem with your code is that in each of the 7 iterations of the loop you are assigning the hints to all the buttons, so you are doing 7 assignments to each button, total 49 assignments, increasing the date after each assignment, so you reach these incorrect dates.
So the result is that you see the values assigned from the last iteration which are obviously wrong.
Do 1 assignment to 1 button in each iteration like this:

DateTimeFormatter dateFormater = DateTimeFormatter.ofPattern("d");
ZoneId zone = ZoneId.of("Asia/Jakarta");
LocalDate date = LocalDate.now(zone);
int amount = 1;
int buttonCount = 7;
for (int i = 0; i < buttonCount; i++){
    int buttonId = getResources().getIdentifier("hari_" + (i + 1), "id", getPackageName()); 
    Button button = (Button) findViewById(buttonId);
    button.setHint(date.format(dateFormater));
    date = date.plusDays(amount);
}

With this line:

int buttonId = getResources().getIdentifier("hari_" + (i + 1), "id", getPackageName());

you get the integer id of each button and with this line:

Button button = (Button) findViewById(buttonId);

you get a variable referencing the button.

forpas
  • 160,666
  • 10
  • 38
  • 76
  • what do i do with 'n' ? it's eror cannot resolve symbol. should i create new variable int for that ? – avid _ kupit Jan 21 '20 at 21:25
  • i got eror on the line `button.setHint(date.format(dateFormater));` **void android.widget.Button.setHint(java.lang.CharSequence)' on a null object reference** date value is null ? why ? – avid _ kupit Jan 21 '20 at 21:29
  • `hari1 = (Button)findViewById(R.id.hari_1);` id in xml `hari_1`,`hari_2`,`hari_3`,ect. – avid _ kupit Jan 21 '20 at 21:32
2

Using a stream

The Answer by forpas is correct. For fun here, let's try using a stream. I am not claiming this is better than the other first solution, just an alternative.

We use an IntStream to count to seven. We use the produced integer to add a number of days to our current date, and to access a specific button by using naming approach shown in that other Answer.

Rather than go through a DateTimeFormatter to get a day-of-month, we call LocalDate::getDayOfMonth. Convert from int to string via String.valueOf.

I've not tried running this code as I do not do Android work. But hopefully it is close to working.

ZoneId z = ZoneId.of( "Asia/Jakarta" ) ;
LocalDate today = LocalDate.now( z ) ;

IntStream
.range( 0 ,  7 )
.forEach( 
    i -> 
    ( 
        (Button) findViewById( 
            getResources().getIdentifier( "hari_" + (i + 1), "id" , getPackageName() )   // See: https://stackoverflow.com/a/59849331/642706  
        ) 
    )                         // Returns a `Button`.
    .setHint ( 
        String.valueOf(       // Convert int (day-of-month) to text.
            today
            .plusDays( i )    // Produces a new `LocalDate` object rather than modifying the original (immutable objects). 
            .getDayOfMonth()  // Returns an `int`. 
        )
    )  
    ;
)
;
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154