I’m looking for a reference example of how to read times in a list, and return the one that occurs “next.”
I’m working on a schedule app, but currently I’m doing a very nested bunch of if statements that look something like this:
if (Time.compare(bus1.getTime(), currentTime) > 0) {
setTimeLabelLOY(bus1);
} else {
if (Time.compare(bus2.getTime(), currentTime) > 0) {
setTimeLabelLOY(bus2);
} else {
if (Time.compare(bus3.getTime(), currentTime) > 0) {
setTimeLabelLOY(bus3);
} else {
if (Time.compare(bus4.getTime(), currentTime) > 0) {
setTimeLabelLOY(bus4);
} else {
if (Time.compare(bus5.getTime(), currentTime) > 0) {
setTimeLabelLOY(bus5);
} else {
if (Time.compare(bus6.getTime(), currentTime) > 0) {
setTimeLabelLOY(bus6);
Obviously my Bus class contains Time elements that I have hardcoded. I would prefer to simply read a list of times and output whichever one is next. Or maybe make an array of times and have them sorted based on the current time?
Any advice or examples on how to do this better would be greatly appreciated.
If you store all of your buses in a List you could use:
You could also store all of the bus data in a SQLite database and then use a simple query like this: