Dan
Dan

Reputation: 89

Trying to get listview to work

My question is how do I fix my ListView as it doesn't show anything

So in my main file I have

 private ObservableList<Customer> customers = FXCollections.observableArrayList();

two customers are added

Customer kelly = addCustomer("95432123", "Kelly Lee");
Customer tim = addCustomer("92345678", "Tim Williams");

I have a method for returning the ObservableList

public final ObservableList<Customer> getCustomers() {
    return customers;
}

on my XML file I have a listview linking to the controller via

<ListView fx:id="namelistview" layoutX="159.0" layoutY="8.0" prefHeight="200.0" prefWidth="200.0" />

So I am stuck on my controller trying to actually have data printed into the ListView in the form

Kelly Lee: 95432123

I currently have this in my Controller file but my listview is still showing up as blank

public class PizzeriaController extends Controller<Pizzeria> {
ObservableList<Customer> customers = FXCollections.observableArrayList();

@FXML
private ListView<Customer> namelistview;

public void namelistview() {
    namelistview.setItems(getPizzeria().getCustomers());
    }



public void initialize() {
}

public final Pizzeria getPizzeria() {
    return model;
}
}

Upvotes: 0

Views: 59

Answers (1)

SedJ601
SedJ601

Reputation: 13859

The namelistview.setItems method needs an ObservableList as input, so change:

namelistview.setItems(getPizzeria().getCustomers());

to:

namelistview.setItems(customers);

and put this code inside the initialize method:

public void initialize() {
    namelistview.setItems(customers);
}

Now change:

public void namelistview() {
     namelistview.setItems(getPizzeria().getCustomers());
}

to:

public void namelistview() {
     customers.add(getPizzeria().getCustomers());
}  

Upvotes: 1

Related Questions