簡體   English   中英

將 arraylist 的第一個元素分配給另一個 arraylist

[英]Assigning first element of arraylist to another arraylist

如何從另一個 class 獲取 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();

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









    }
}

然后將 cus 數組分配給列表視圖?

任何幫助將不勝感激謝謝

嘗試:

cusarray.add(allcustomers.get(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 的代碼,它按預期工作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM