Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/395.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 为什么ListView显示对象地址而不是内容?_Java_Listview_Javafx_Scenebuilder - Fatal编程技术网

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

Java 为什么ListView显示对象地址而不是内容?,java,listview,javafx,scenebuilder,Java,Listview,Javafx,Scenebuilder,我有一个列表视图和一个名为参与者的类。我想在此列表视图中将对象的内容显示为项目,但它在列表视图中显示为地址: 这是我填充ListView的代码。我怎样才能解决这个问题 package view_FXML; import Model.Curse; import Model.Participant; import Service.GeneralService; import javafx.collections.FXCollections; import javafx.collections.O

我有一个
列表视图
和一个名为
参与者
的类。我想在此
列表视图
中将对象的内容显示为项目,但它在列表视图中显示为地址:

这是我填充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(){
 // }
}
包视图\u FXML;
进口模式。诅咒;
导入模型。参与者;
导入服务.一般服务;
导入javafx.collections.FXCollections;
导入javafx.collections.ObservableList;
导入javafx.fxml.fxml;
导入javafx.scene.control.ListView;
导入javafx.scene.control.cell.ComboBoxListCell;
导入javafx.scene.control.cell.PropertyValueFactory;
公共类SearchViewController{
@FXML
私有列表视图列表视图;
私人一般服务;
私有可观察者模型;
公共无效设置服务(一般服务服务){
服务=服务;
//this.model=FXCollections.observearraylist(service.vizalizareparticipanti());
参与者p=新参与者(1,“Laszlo”,1100,2);
参与者p2=新参与者(2,“John”,2123100);
this.model=FXCollections.observearraylist(p,p2);
setItems(模型);
}
//公共无效初始化(){
// }
}

您需要设置
列表视图的
设置单元工厂

例如:

listView.setCellFactory(新回调(){
@凌驾
公共ListCell调用(ListView参数){
ListCell=新ListCell(){
@凌驾
受保护的void updateItem(参与者项,布尔值为空){
super.updateItem(项,空);
如果(项!=null){
setText(item.getId()+”:“+item.getName()+”+item.etc());
}否则{
setText(“”);
}
}
};
返回单元;
}
});
完整代码:

包视图\u FXML;
进口模式。诅咒;
导入模型。参与者;
导入服务.一般服务;
导入javafx.collections.FXCollections;
导入javafx.collections.ObservableList;
导入javafx.fxml.fxml;
导入javafx.scene.control.ListView;
导入javafx.scene.control.cell.ComboBoxListCell;
导入javafx.scene.control.cell.PropertyValueFactory;
公共类SearchViewController{
@FXML
私有列表视图列表视图;
私人一般服务;
私有可观察者模型;
公共无效设置服务(一般服务服务){
服务=服务;
//this.model=FXCollections.observearraylist(service.vizalizareparticipanti());
参与者p=新参与者(1,“Laszlo”,1100,2);
参与者p2=新参与者(2,“John”,2123100);
this.model=FXCollections.observearraylist(p,p2);
setItems(模型);
setCellFactory(新回调(){
@凌驾
公共ListCell调用(ListView参数){
ListCell=新ListCell(){
@凌驾
受保护的void updateItem(participanItem,布尔空){
super.updateItem(项,空);
如果(项!=null){
setText(item.getId()+”:“+item.getName()+”+item.etc());
}否则{
setText(“”);
}
}
};
返回单元;
}
});
}
//公共无效初始化(){
// }
}

我没有在我的
IDE
中测试这一点,因此在搜索
setCellFactory()
的代码中可能有错误;
    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;
        }
    });
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(){
 // }
}