Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Android

Imran Asif
Imran Asif
7,514 Points

String[] days= {"Sunday","Monday","Tuesday","Wednesday"}; How to get next and previous values

String[] days= {"Sunday","Monday","Tuesday","Wednesday"}; How to get next and previous values on button cllick

I am trying to get next value on next button click and previous values on back button click String[] days= {"Sunday","Monday","Tuesday","Wednesday"}; what is the best way to achieve this ?

i tried index for loop but cannot decreament. For example: if I set currentday= days[0]; // to get next value for(int i=0; i<days.length; i++){ currentday=days[i]; // to get previous value cannot use --

Binyamin Friedman
Binyamin Friedman
14,615 Points

Your question is a little confusing, could you elaborate, or show existing code?

It sounds like you want to make a simple app, with a text view, and two buttons. If this is the case, then I would suggest keeping a local field/variable that will store the array index, starting at 0.

You would set an onClickListener for each button; one would increment the field, and one would decrement the field. Make that you use if statements to make sure that the field never goes below 0 or above the length of the array.

Each time that a button is clicked you would also call a private method that would update the textView to the correct index of the array.

1 Answer

Imran Asif
Imran Asif
7,514 Points

Thanks Binyamin Friedman, I did the same, I have declared one variable Type int and initialize it with zero then used with if statement and increased int with ++ it worked for me.