0

How do i update ListView items when i added a new data. Here is my code for displaying data to my listview:

public class ChapterController implements Initializable {

@FXML
private ListView<String> listview;
ObservableList<String> view = FXCollections.observableArrayList();

@FXML
private TextField des;
@FXML
private TextField no;

Connection connection = null;
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;

public ChapterController() {
connection = Connect.ConnectDb();
}
@Override
public void initialize(URL url, ResourceBundle rb) {
    listview.setItems(view);
    ListView()
}  

//display data to the listview
private void ListView(){
    String sql = "SELECT * FROM chapter ORDER BY id ASC";
    try{
        preparedStatement = connection.prepareStatement(sql);
        resultSet = preparedStatement.executeQuery();

        while(resultSet.next()){
              view.add("Chapter" +" "+ resultSet.getString(1) +" "+ "-" +" "+ resultSet.getString(2));
        }
    }catch(Exception e){

    }
}

And here is my code for adding item on the list view, and I want to automatically updated the listview when I save the data. Where should I put my code for updating the listview.

public void buttonAction(ActionEvent event){
    connection = Connect.ConnectDb();
    try{
        String sql = "INSERT INTO chapter"
            +"(id, description)"
               +"VALUES(?,?)";

        preparedStatement = (PreparedStatement) connection.prepareStatement(sql);
        preparedStatement.setString(1, no.getText());
        preparedStatement.setString(2, des.getText());
        preparedStatement.executeUpdate();
        ListView();  //I am using to call the ListView but it is not good at all
    }catch(Exception e){

    }
}
sj_abaquita
  • 99
  • 1
  • 8
  • Why don't you just do `view.add(...)` in the event handler, instead of calling `ListView()` again (which will add all the items in the database, not just the new one). – James_D Sep 11 '17 at 12:34
  • I already do that but it doesn't work – sj_abaquita Sep 11 '17 at 12:43
  • What does "it doesn't work" mean? What actually happens? Also, don't use empty `catch` blocks: you should, at a minimum, log the stack trace of any exceptions (otherwise you have no chance of knowing if something is going wrong). – James_D Sep 11 '17 at 13:00
  • java.sql.SQLException: After end of result set at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:957) at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:896) ........ – sj_abaquita Sep 11 '17 at 13:15
  • So now you can read the [stack trace](https://stackoverflow.com/q/3988788/2775450) and figure out what is wrong. – James_D Sep 11 '17 at 13:16
  • Also, are you saying you're getting that exception if you call `view.add(...)` instead of calling `ListView()` in the event handler (as I suggested). That doesn't make sense, because you don't have a result set in that case. – James_D Sep 11 '17 at 13:20
  • Great!.. .Thank you @James_D – sj_abaquita Sep 11 '17 at 13:36

0 Answers0