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

标签 java listview javafx scenebuilder

我有一个 ListView 和一个名为 Participant 的类。我想在此 ListView 中将对象的内容显示为项目,但它在 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的 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 中对此进行测试,因此代码中可能存在错误

关于java - 为什么ListView显示对象地址而不是内容?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49541293/

相关文章:

java - 在对象之间放置空格的最佳方法是什么? Swing JSeparator 对象可以是不可见的分隔符吗?

c# - 是否可以从 ListView 拖放到 Winforms 中的 TreeView?

JavaFX和Canvas的快速更新

java - 手动展开/折叠所有树项内存成本javafx 2.2

java - 更改 Hibernate 默认表结构

java - 如何在java中计算curve1174的加倍?

java - 解密 DES/CBC/ZeroBytePadding 数据

wpf - 在wpf mvvm中成功保存后如何自动更新 ListView 中的选定行

android appwidget listview 不更新

JavaFX - 更改 FlowPane 中子项的顺序