JavaFX Listview将按钮绑定到单元格的索引

时间:2014-04-05 17:35:04

标签: java listview javafx custom-cell

我尝试使用JavaFX构建自定义视图,允许将2个按钮“编辑用户”和“查看用户”放置在单元格中。

我已经构建了适配器,但是在创建单元格时我无法为按钮分配索引,这是一个问题,因为当我单击单元格中的按钮时,我得到空指针挖掘。

编辑07/04/2014:我已经更新了我的程序逻辑但我仍然在努力寻找解决我的问题的方法 - 分配的索引有时是正确的,但大多数是不正确的(我设置了一个日志来给出我当前的指数,它波动很大,这导致我得出结论我的单元格构造函数是不可靠的)

我现在使用以下代码:populateListView():

public void populateListView() {

    // Zero the index each time the list view is repopulated to 
    // bind to the correct button
    index = 0;

    // Refresh the lists contents to null
    lstNotes.setItems(null);

    // Observable list containing items to add
    ObservableList notes = populateObservable(thisTenant.getNotes());

    // Set the items returned by populateObservable(T);
    lstNotes.setItems(notes);
    lstNotes.setFixedCellSize(50);

    // Use a cell factory for custom styling
    lstNotes.setCellFactory(new Callback<ListView, ListCell>() {
        @Override
        public ListCell call(ListView listView) {
            NoteCell nCell = new NoteCell(index, controller);
            index++;
            nCell.getStyleClass().add("border-bottom");
            return nCell;
        }
    });
}

这里的注释相当自我解释 - 通过这种方法填充了一个可观察的项目列表 - populateObservable(NoteList n):

public ObservableList populateObservable(ArrayList<Note> notesArray) {

    ObservableList notes = FXCollections.observableArrayList();

    // Loop through the users Notes array and create a listview item
    for(int i = 0; i < notesArray.size(); i++) {
        Note thisNote = notesArray.get(i);
        notes.add(thisNote.getMessage());
    }

    return notes;
}

最后自定义视图是在我的单元工厂中构建的(样式与问题无关),应该注意的是对我的Controller的引用传递给它所以我可以访问方法 - 在NoteCell类中我有一个删除给定索引处的行的方法(这是产生偏差结果的方法)

 cross.setOnMouseClicked(new EventHandler<MouseEvent>() {
      @Override
      public void handle(MouseEvent mouseEvent) {
          ScreensFramework.logGeneral.writeToFile("I am removing from index " + String.valueOf(thisIndex));
          if(thisTenant.removeNoteAt(thisIndex, true)){
              controller.populateListView();
          } else {
              controller.populateListView();
          }
      }
  });

对populateListView()的调用只是刷新列表视图的内容并根据租户更新的Note数组重建它。

RemoveNoteAt()方法向我的数据库发送一个删除查询,并从给定索引处的租户注释数组中删除注释(但我并不总是得到如上所述的正确索引,因为我找不到有效的将按钮绑定到ListViews单元格的方法和我传递索引的微弱尝试似乎不起作用)。

我希望此更新为我的问题提供更清晰的范围,如果您需要更多信息以请更清楚地了解此问题,请提出请求。

编辑:NoteCell代码:

public class NoteCell extends ListCell<String> {
HBox hbox = new HBox();
HBox c = new HBox();
Pane b = new HBox();
Pane pane = new Pane();
Label msg = new Label();
Label date = new Label();
Button cross = new Button();
Tenant thisTenant;
int thisIndex;

String lastItem;


public NoteCell(int index, UserInfoController controller) {
    super();

    try{
        thisTenant = controller.getTenant();
        Note currNote = thisTenant.getNoteAt(index);
        date = new Label(currNote.getDate());
        System.out.println(thisTenant.getNotes());
        thisIndex = index;

        date.setStyle("-fx-text-fill: #fff !important; -fx-font-family: Helvetica; -fx-font-weight: bold; -fx-font-size: 12px; -fx-background-color: #f9246b; -fx-border-color: #FE246C; -fx-padding: 8 8 6 8; -fx-border-radius: 6; -fx-background-radius: 6");
        b.setStyle("-fx-padding: 10px 5px 10px 5px");
        msg.setStyle("-fx-text-fill: #fafafa; -fx-font-size: 14px; -fx-font-family: 'Open Sans Light'; -fx-label-padding: 10px");
        cross.getStyleClass().add("button_close");
        cross.setTranslateY(20);
        msg.setAlignment(Pos.CENTER_LEFT);

        pane.setTranslateY(20);
        b.getChildren().add(date);
        c.getChildren().add(cross);
        hbox.getChildren().addAll(b, msg, pane, cross);
        HBox.setHgrow(pane, Priority.ALWAYS);


        // Deletes the note
        cross.setOnMouseClicked(new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent mouseEvent) {
                ScreensFramework.logGeneral.writeToFile("I am removing from index " + String.valueOf(thisIndex));
                if(thisTenant.removeNoteAt(thisIndex, true)){
                    controller.populateListView();
                } else {
                    controller.populateListView();
                }
            }
        });

    } catch (Exception e) {
        ScreensFramework.logError.writeToFile(e.getMessage());
    }
}



@Override
protected void updateItem(String item, boolean empty) {

    super.updateItem(item, empty);
    setText(null);  // No text in label of super class
    if (empty) {
        lastItem = null;
        setGraphic(null);
    } else {
        lastItem = item;
        msg.setText(item!=null ? item : "<null>");
        setGraphic(hbox);
    }
}

}

编辑:在创建ListView单元格时有什么办法可以将该单元格的当前索引传递给“CellFactory”吗?

此致 亚历

1 个答案:

答案 0 :(得分:2)

每个单元格都知道它的索引:

public class Demo extends Application {

    ListView<String> list = new ListView<String>();

    ObservableList<String> data = FXCollections.observableArrayList(
            "chocolate", "salmon", "gold", "coral", "darkorchid",
            "darkgoldenrod", "lightsalmon", "black", "rosybrown", "blue",
            "blueviolet", "brown");

    @Override
    public void start(Stage stage) {
        VBox box = new VBox();
        Scene scene = new Scene(box, 200, 200);
        stage.setScene(scene);
        box.getChildren().addAll(list);
        VBox.setVgrow(list, Priority.ALWAYS);

        list.setItems(data);

        list.setCellFactory(new Callback<ListView<String>, ListCell<String>>() {
            @Override
            public ListCell<String> call(ListView<String> list) {
                return new ButtonCell();
            }
        });
        stage.show();
    }

    static class ButtonCell extends ListCell<String> {
        @Override
        public void updateItem(String item, boolean empty) {
            super.updateItem(item, empty);
            if (item != null) {
                final Button btn = new Button(item + " with index " + getIndex());
                btn.setOnAction(new EventHandler<ActionEvent>() {
                    @Override
                    public void handle(ActionEvent event) {
                        System.out.println("I am a " + getItem() + " with index " + getIndex());
                    }
                });
                setGraphic(btn);
            } else {
                setGraphic(null);
            }
        }
    }

    public static void main(String[] args) {
        launch(args);
    }
}