我有一个名为“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”过滤。这是我现在正在工作的另一个班级:
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的属性进行过滤?
如果我不清楚或代码太多,请提前抱歉,但我想我只是提出解释问题的必要条件。
答案 0 :(得分:1)
您应该将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
属性是否包含过滤器文本,但您可能希望将其更改为其他属性。