Cache the id value sequence. Something along the lines of:
List pictureId = (new ArrayList() );
// Add Ids to list from ResultSet.
pictureId.add("id1");
pictureId.add("id2");
pictureId.add("id3"); // Whatever type your id is?
// etc.
int pictureIdIndex = 0; // Lists are zero indexed.
// if previous was pressed.
pictureIdIndex -= 1;
if (pictureIdIndex < 0) pictureIdIndex = 0;
// if next was pressed.
pictureIdIndex += 1;
if (pictureIdIndex > (pictureId.size() - 1) ) {
pictureIdIndex = (pictureId.size() - 1);
}
// To get the current id.
String currentId = (String)pictureId.get(pictureIdIndex);
Haven't compiled or tested this, but you get the idea.