D
D
Drunya20182018-10-22 23:20:50
Java
Drunya2018, 2018-10-22 23:20:50

JavaFx. Why, after adding two different elements, 2 identical ones are added to the collection?

@FXML
void drawUi() {
ObservableList usersData = FXCollections.observableArrayList();
okeyButton.setOnAction(event -> {
Main.service.submit(() -> {
ArrayList id = words(textField.getText());
for (int i = 0; i < id.size(); i++) {
try {
ResponseGlobalInfo responseInfo = ShowInfo.getInfo(id.get(i));
for (int j = 0; j < responseInfo.items.length; j++) {
Channel.setName(responseInfo.items[j].snippet.title);
Stringname = Channel.getName();
Channel.setDate(responseInfo.items[j].snippet.publishedAt);
String publishedDate = Channel.getDate();
String date = GlobalInfo.getDate(publishedDate);
Channel.setSubscribers(responseInfo.items[j].statistics.subscriberCount);
String subscribers = Channel.getSubscribers();
Channel.setCountVideo(responseInfo.items[j].statistics.videoCount);
String countVideo = Channel.getCountVideo();
Channel.setCountViews(responseInfo.items[j].statistics.viewCount);
String countViews = Channel.getCountViews();
usersData.add(i, new Channel(Channel.getName(), Channel.getDate(), Channel.getSubscribers(),
Channel.getCountVideo(), Channel.getCountViews()));
System.out.println(" id - " + id.get(j));
System.out.println("name - " + name);
}
treeView.setVisible(true);
nameColumn.setCellValueFactory(new PropertyValueFactory("name"));
dateColumn.setCellValueFactory(new PropertyValueFactory("date"));
subColumn.setCellValueFactory(new PropertyValueFactory("subscribers"));
videoColumn.setCellValueFactory(new PropertyValueFactory("countVideo"));
viewsColumn.setCellValueFactory(new PropertyValueFactory("countViews"));
treeView.setItems(usersData);
} catch (UnirestException e) {
e.printStackTrace();
}
}
});
});
}
here is the code, I can throw it on github.
After executing the method, the data of two YouTube channels should appear in the columns. As a result, data of 1 channel is output 2 times. In the body of the cycle, everything goes ok and the data is different, but after adding, 2 identical channels appear in userData. What could be the problem?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
SeniorJD, 2018-10-23
@SeniorJD

replace i with j in the string

usersData.add(i, new Channel(Channel.getName(), Channel.getDate(), Channel.getSubscribers(),

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question