• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

ListView with IDs?

 
Matt Novak
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm populating ListView with people from sql database.
I'd like to assign an ID to each person in list so I could fetch data from DB after person is clicked- something similar as in html (<option value="X">Name surname</option>).

As far as I can see it's not possible and the only way is using list.getSelectionModel().getSelectedIndex() to get order of selected item. Is that correct? Name (.getSelectedItem) doesn't help me as it's not unique!

How can I then transform name into sql ID so I could fetch selected person's data?
I'm thinking of creating TWO arrays - one ObservableList to fill ListView and another int[] with IDs only (same order) but that is really not optimal.

Do you have any ideas or hints?
 
Matt Novak
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Answering myself
I created a new class with people:

And then instead of ObservableList of strings, I create:

ObservableList<Person> choices = FXCollections.observableArrayList();
for (....) choices.add(new Respondent(ID, NAME));

...

And I get ID with:
etSelectionModel().getSelectedItem().id :-)
 
Matt Novak
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK, one more question.. if I wanted to add another parameter, let's say Address to each person so I could switch between list of names and list of addresses.

The first part- extending class with another String and adding two strings goes well:



But the problem is that override toString only returns one thing (name) so when I create listview I get whatever toString returns :
final ListView<Person> lv = new ListView<>(choices);

How could I add another method / override to get list od addressess from same object?
 
John Damien Smith
Ranch Hand
Posts: 296
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Use a custom cell factory
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic