将数组列表的第一个元素分配给另一个数组列表。

huangapple 未分类评论47阅读模式
英文:

Assigning first element of arraylist to another arraylist

问题

如何从另一个类获取ArrayList的第一个元素并将其赋值给另一个ArrayList

public class PaymentScreenController {

    public ListView<Customer> lvCustomer1;
    public ArrayList<Customer> allcustomers;

    public ArrayList<Customer> cusarray;

    private Table tbl = new Table();

    @FXML
    public void initialize() {

        allcustomers = tbl.getCustomers();

        // 从'allcustomers'获取第一个元素并赋值给cusarray?
        // 我已经尝试过 cusarray = allcustomers.get(0),但那行不通?

    }
}

然后将cusarray赋值给一个ListView?

如有帮助,感激不尽。谢谢!

英文:

How do I get the first element of an ArrayList from another class and assign it to another ArrayList?

public class PaymentScreenController {

    public ListView&lt;Customer&gt; lvCustomer1;
    public ArrayList&lt;Customer&gt; allcustomers;

    public ArrayList&lt;Customer&gt; cusarray;


    private Table tbl = new Table();

    @FXML
    public void initialize() {

        allcustomers = tbl.getCustomers();

        // Getting first element from &#39;allcustomers and assigning it to cusarray?
        // I have tried cusarray = allcustomers.get(0) but that doesn&#39;t work?









    }
}

And then assign cus array to a listview?

Any help would be appreciated thanks

答案1

得分: 0

尝试
cusarray.add(allcustomers.get(0));
英文:

try:

cusarray.add(allcustomers.get(0));

答案2

得分: 0

你可以初始化 cusarray 并将项目添加到列表中:

cusarray = new ArrayList<>();
cusarray.add(allcustomers.get(0));

这对你应该有效。

然后对于 ListView,我想你可能想要类似这样的代码:

lvCustomer1 = new ListView(FXCollections.observableArrayList(cusarray));
lvCustomer1.setCellFactory(param -> new ListCell<Customer>() {
    @Override
    protected void updateItem(Customer item, boolean empty) {
        super.updateItem(item, empty);

        if (empty || item == null || item.<function to get name>() == null) {
            setText(null);
        } else {
            setText(item.<function to get name>());
        }
    }
});

虽然我还没有测试过 ListView 的部分。

编辑:现在已经测试了 ListView 的代码,它按预期工作。

英文:

You can initialize the cusarray and add the item to the list:

cusarray = new ArrayList&lt;&gt;();
cusarray.add(allcustomers.get(0);

That should work for you.

Then for the ListView, I guess you'd want something like this:

lvCustomer1 = new ListView(FXCollections.observableArrayList(cusarray));
lvCustomer1.setCellFactory(param -&gt; new ListCell&lt;Customer&gt;() {
@Override
protected void updateItem(Customer item, boolean empty) {
    super.updateItem(item, empty);

    if (empty || item == null || item.&lt;function to get name&gt;() == null) {
        setText(null);
    } else {
        setText(item.&lt;function to get name&gt;());
    }
}
});

Though I haven't tested the ListView bit.

EDIT: Now tested the code for the ListView and it works as intended.

huangapple
  • 本文由 发表于 2020年4月6日 21:12:06
  • 转载请务必保留本文链接:https://java.coder-hub.com/61060645.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定