为什么 ListView 显示对象地址而不是内容?

Why ListView displays Objects addresses instead of contents?

我有一个 ListView 和一个名为 Participant 的 class。我想将对象的内容显示为这个 ListView 中的项目,但地址显示在列表视图:photo.

这是我填充 ListView 的代码。我该如何解决这个问题?

package view_FXML;

import Model.Curse;
import Model.Participant;
import Service.GeneralService;

import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.scene.control.ListView;
import javafx.scene.control.cell.ComboBoxListCell;
import javafx.scene.control.cell.PropertyValueFactory;

public class SearchViewController {
    @FXML
    private ListView<Participant> listView;

private GeneralService service;
private ObservableList<Participant> model;

public void setService(GeneralService service){
    this.service=service;
    //this.model= FXCollections.observableArrayList(service.vizualizareParticipanti());
    Participant  p = new Participant(1,"Laszlo",1,100,2);
    Participant p2 = new Participant(2,"John",2,123,100);
    this.model= FXCollections.observableArrayList(p,p2);
    listView.setItems(model);
}
 // public void initialize(){
 // }
}

您需要设置 ListView's setCellFactory.

Example:

    listView.setCellFactory(new Callback<ListView<Participant>, 
    ListCell<Participant>>() {

        @Override
        public ListCell<Participant> call(ListView<Participant> param) {
            ListCell<Participant> cell = new ListCell<Participant>() {

                @Override
                protected void updateItem(Participant item, boolean empty) {
                    super.updateItem(item, empty);
                    if (item != null) {
                        setText(item.getId() + ": " + item.getName() + " " + item.etc());
                    } else {
                        setText("");
                    }
                }
            };
            return cell;
        }
    });

Full code:

package view_FXML;

import Model.Curse;
import Model.Participant;
import Service.GeneralService;

import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.scene.control.ListView;
import javafx.scene.control.cell.ComboBoxListCell;
import javafx.scene.control.cell.PropertyValueFactory;

public class SearchViewController {
    @FXML
    private ListView<Participant> listView;

private GeneralService service;
private ObservableList<Participant> model;

public void setService(GeneralService service){
    this.service=service;
    //this.model= FXCollections.observableArrayList(service.vizualizareParticipanti());
    Participant  p = new Participant(1,"Laszlo",1,100,2);
    Participant p2 = new Participant(2,"John",2,123,100);
    this.model= FXCollections.observableArrayList(p,p2);
    listView.setItems(model);
    listView.setCellFactory(new Callback<ListView<Participant>, 
    ListCell<Participant>>() {

        @Override
        public ListCell<Participant> call(ListView<Participant> param) {
            ListCell<Participant> cell = new ListCell<Participant>() {

                @Override
                protected void updateItem(Participantitem, boolean empty) {
                    super.updateItem(item, empty);
                    if (item != null) {
                        setText(item.getId() + ": " + item.getName() + " " + item.etc());
                    } else {
                        setText("");
                    }
                }
            };
            return cell;
        }
    });
}
 // public void initialize(){
 // }
}

我没有在我的IDE中测试这个,所以代码中可能有错误