Java telegram bot
I try to build class that can ask questions from repository and provide answers from another repository with InLineKeyboard. I have many answers for each question, can get them with Preicate var answers = answerRepository.findAll(new AnswerSpecification(model)); (it`s already works fine). The problem is loop thath builds keyboard.
i want it to be build depending on how many answers do repository have, but not more than 3 in line.enter image description here
Main problem is that it sets each button with 1st element in List, how can i fix it? I want each next button have next element from repo.
private void askQuestion(Long chatId, Long questionId) {
var question = questionRepository.findById(questionId).get().getText();
SendMessage message = new SendMessage();
message.setChatId(String.valueOf(chatId));
message.setText(question);
InlineKeyboardMarkup markupLine = new InlineKeyboardMarkup();
List<List<InlineKeyboardButton>> rowsInLine = new ArrayList<>();
List<InlineKeyboardButton> rowInLine = new ArrayList<>();
var button = new InlineKeyboardButton();
Criteria model = new Criteria("questionId", Operation.EQ, String.valueOf(questionId));
var answers = answerRepository.findAll(new AnswerSpecification(model));
// count of all answers got by questionId
int answers_count = answers.size();
// count of max rows
int rows_count = answers_count / 3;
# for (Answer an : answers) {
# while (rowsInLine.size() < rows_count) {
# while (rowInLine.size() < 3) {
# button.setText(an.getText());
# button.setCallbackData(an.getText());
# rowInLine.add(button);
# }
# rowsInLine.add(rowInLine);
# }
# }**
markupLine.setKeyboard(rowsInLine);
message.setReplyMarkup(markupLine);
executeMessage(message);
}
I tried to replace while`s and for (Answer an : answer) but this is my best try.
Petr Goldberg is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.