I've added a textfield to my GUI and I want it to filter through the host names if a name in inputed. E.g.: "Amy" would display all the Amy's in the csv file renting a house in the ListCell. I've added a method searchByHost() where I'd like to do it.
Here is my code:
public class ListingsController
{
private ObservableList<String> comboBoxList = FXCollections.observableArrayList("Host Name: A-Z",
"Host Name: Z-A", "Price: Low to High", "Price: High to Low", "No of Reviews: Low to High",
"No of Reviews: High to Low");
private List<AirbnbListing> propertyArrayList;
private ObservableList<AirbnbListing> propertyList;
private SortedList<AirbnbListing> sortedList;
public TextField inputHostName;
@FXML
private ComboBox<String> comboBox;
@FXML
private ListView<AirbnbListing> listingsList;
@FXML
private TextField searchField;
private String name;
private String min;
private String max;
// TODO: remove at some point
static final List<AirbnbListing> data = AirbnbDataLoader.load("airbnb-london.csv");
public void initialize()
{
comboBox.setItems(comboBoxList);
comboBox.valueProperty().addListener(e -> sortListings());
setListings(data);
listingsList.setCellFactory(new Callback<ListView<AirbnbListing>, ListCell<AirbnbListing>>() {
@Override
public ListCell<AirbnbListing> call(ListView<AirbnbListing> airbnbListingListView) {
return new ListCell<AirbnbListing>() {
@Override
protected void updateItem(AirbnbListing listing, boolean empty) {
super.updateItem(listing, empty);
if (listing != null) {
setText(listing.getHost_name() " - " listing.getPrice() " - " listing.getNumberOfReviews() " - " listing.getMinimumNights());
} else {
setText("");
}
}
};
}
});
listingsList.getSelectionModel().selectedItemProperty().addListener((o, v0, v1) -> displayListing(v1));
}
public void setListings(List<AirbnbListing> listings)
{
listingsList.getItems().addAll(listings);
}
public void sortListings()
{
}
public void displayListing(AirbnbListing listing)
{
if (listing == null)
return;
System.out.println(listing);
}
public void searchByHost()
{
}
}
All the examples I've seen online use a filter an observable list however I don't think I'd need that.
CodePudding user response:
CodePudding user response:
You should have two observable lists. One containing the filtered data and one containing all the data.
For filtering the list you can use the Java Stream API