![](/img/trans.png)
[英]Get a random element from ArrayList, add to another ArrayList and delete the element from first ArrayList Android
[英]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.