gpt4 book ai didi

java - 带有自定义对象的 FIlteredList

转载 作者:行者123 更新时间:2023-12-01 10:34:35 26 4
gpt4 key购买 nike

我有一个名为“MyObject”的类,如下所示:

public class MyObject {
private int type;
private byte[] file;
private String fileName;
private String titolo;

public MyObject(String tit, int t, byte[] b, String name)
{
titolo= tit;
type = t;
file = b;
fileName = name;
}


int getType(){
return type;
}
String getfileName(){
return fileName;
}
byte[] getFile(){
return file;
}
String getTitolo(){
return titolo;
}
}

我已经开发了如何加载列表的元素,现在,我想用 FilterList 进行过滤。我想按“titolo”和“fileName”进行过滤。这是我现在正在工作的另一个类(class):

public class ContentTab extends VBox{
Label allegati;
TextField inseriscitesto;
ListView<MyObject> view;
ObservableList<MyObject> data;

public ContentTab(int width){

setPadding(new Insets(20));
setSpacing(10);
allegati = new Label("Allegati alla lezione");
allegati.setFont(Font.font("Verdana", 13));
inseriscitesto = new TextField("");
data = FXCollections.observableArrayList();

view = new ListView<MyObject>(data);

view.setStyle("-fx-background-color: transparent;");
setPrefWidth(width);
getChildren().addAll(allegati,inseriscitesto, view);


}

public void loadElement(List<MyObject> element){

for(int i = 0; i< element.size(); i++)
data.add(element.get(i));

view.setCellFactory(new Callback<ListView<MyObject>, ListCell<MyObject>>() {

@Override
public ListCell<MyObject> call(ListView<MyObject> arg0) {
return new ListCell<MyObject>() {

@Override
protected void updateItem(MyObject item, boolean bln) {
super.updateItem(item, bln);
if (item != null) {
setStyle("-fx-background-color: transparent;");
VBox vBox = new VBox(new Text(item.getTitolo()), new Text(item.getfileName()));
ImageView placeHolder;

if(item.getType() == myMediaType.PDF){
placeHolder= new ImageView(new Image("img/pdf.jpg"));
placeHolder.setScaleX(0.5);
}
else
placeHolder= new ImageView(new Image("img/video.jpg"));
HBox hBox = new HBox(placeHolder, vBox);

hBox.setSpacing(10);
setGraphic(hBox);
}
}

};
}

});
}

public void FilterElement(List<MyObject> element){

FilteredList<MyObject> filteredData = new FilteredList<>(data, s -> true);

inseriscitesto.textProperty().addListener(obs->{
String filter = inseriscitesto.getText();
if(filter == null || filter.length() == 0) {
filteredData.setPredicate(s -> true);
}
else {
filteredData.setPredicate(s -> ((List<MyObject>) s.).contains(filter));
}
});


}

所以,我的问题是,如何在 FilterElement 方法中按 MyObject 的属性进行过滤?

如果我不清楚或代码太多,请提前抱歉,但我认为我只是解释了问题所必需的。

最佳答案

您应该将 ListView 项设置为过滤列表,并保留对它的引用,并随着文本更改而更改其谓词:

public class ContentTab extends VBox{
Label allegati;
TextField inseriscitesto;
ListView<MyObject> view;

public ContentTab(int width){

setPadding(new Insets(20));
setSpacing(10);
allegati = new Label("Allegati alla lezione");
allegati.setFont(Font.font("Verdana", 13));
inseriscitesto = new TextField("");
FilteredList<MyObject> filteredData = new FilteredList(FXCollections.observableArrayList(), s -> true);

view = new ListView<MyObject>(filteredDate);
inseriscitesto.textProperty().addListener(obs->{
String filter = inseriscitesto.getText();
if(filter == null || filter.length() == 0) {
filteredData.setPredicate(s -> true);
}
else {
filteredData.setPredicate(s -> s.getTitolo().contains(filter));
}
});

view.setStyle("-fx-background-color: transparent;");
setPrefWidth(width);
getChildren().addAll(allegati,inseriscitesto, view);

}

另请注意,您的谓词应该是一个接受 MyObject 的函数,如果应该显示,则返回 true,如果应该显示,则返回 false应该被过滤掉。我猜您想检查 Titolo 属性是否包含过滤器文本,但您可能想将其更改为另一个属性。

关于java - 带有自定义对象的 FIlteredList,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34852368/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com