我有ArrayList的问题。我在视图中添加用户,添加了用户但是在关闭窗口AddUserController后我的列表为空。这很奇怪。有代码:
public class AddUserController extends MainController implements Initializable {
private MainPanelController mainControl;
@FXML
private Label UserErrorLabel;
@FXML
private Button UserNameOkButton;
@FXML
private AnchorPane AddUserAnchor;
@FXML
private TextField UserNameTextField;
@Override
public void initialize(URL arg0, ResourceBundle arg1) {
addUser();
}
public void addUser(){
UserNameOkButton.setOnAction(x->{
String message=getModelLogic().addUserToLogic(UserNameTextField.getText());
UserErrorLabel.setTextFill(Color.RED);
UserErrorLabel.setText(message);
});
}
public void setMainControl(MainPanelController mainPanel){
mainControl=mainPanel;
}
}
我的逻辑有类,我使用方法addUserToLogic: 公共类ModelLogic实现了IApplicationLogic {
//***USER LIST, MYSQL, CATEGORYLIST***//
public UserList userList = new UserList();// object of class UserList which handle list of all users
private MySQLDatabase mySQL = new MySQLDatabase();//mySQL object to handle Database
private CategoryList categoryList = new CategoryList();// object of class CategoryList which handle list of all categories
private String msg;
//***CONSTRUCTOR**//
//public ModelLogic(){
//serializeFromFile(categoryList,"CategoryList.obj");
//}
//***GETTERS AND SETTERS***//
public UserList getAllUsers() {
return userList;
}
public MySQLDatabase getMySQL() {
return mySQL;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
//***CREATE DATABASE***//
@Override
public void initDB(){
mySQL.createDatabase();
}
//***HANDLE USERS***//
@Override
public String addUserToLogic(String userName){
User user = new User(userName);
setMsg("User not exist");
Optional<User> userOp = Optional.of(user);
userOp.ifPresent(u->{
boolean isUserAdded=mySQL.addTable(user.getName());
boolean isUserInDb=mySQL.getTableListfromDB().stream()
.anyMatch(l->l.equals(user.getName()));
if(isUserInDb&&isUserAdded){
userList.addComponent(user);
setMsg("User added successfully");
}else{
setMsg("User not added to database");
}
});
return msg;
}
此类实现用户列表和所需方法: 公共类UserList实现IComponentList {
//***CURRENT USER, LIST OF ALL USERS
private User currentUser; // this user is set in program as a current user
private ArrayList<User> usersList= new ArrayList<>();
//GETTERS AND SETTERS
public User getCurrentUser() {
return currentUser;
}
public void setCurrentUser(User presentUser) {
this.currentUser = presentUser;
}
//*This getter for userList is only for intern use in a UserList class*//
public ArrayList<User> getUserList() {
return usersList;
}
public void setUserList(ArrayList<User> userList) {
this.usersList = userList;
}
//***CONSTRUCTORS***//
public UserList(){
};
//***HANDLE USER LIST***//
//*This method return observable list (outside a class) which cointain all users *//
@Override
public ArrayList<User> getComponentList() {
return getUserList();
}
//*This method add user to userList*//
@Override
public void addComponent(User component) {
Optional<User> userOp = Optional.of(component);
userOp.ifPresent(u->{
getUserList().add(component);
});
}
This is class which define one user:
public class User implements IComponent<User>,IComponentList<Goal>, Comparable<User> {
//***NAME, GOALLIST***//
private final String name;
private ArrayList<Goal> goalList;
//***GETTERS AND SETTERS//
@Override
public String getName() {
return this.name;
}
//*This getter for goalList is only for intern use in a User class*//
private ArrayList<Goal> getGoalList() {
return goalList;
}
public void setGoalList(ArrayList<Goal> goalList) {
this.goalList = goalList;
}
//***CONSTRUCTORS***//
public User(String name){
this.name=name;
}
我没有添加所有课程的全部内容。因为有更多的代码。 我通过addusercontroller添加新用户,它被添加到数据库,但我看到当我打开这个窗口并且我使用方法getList()时,我有不同于maincontroller视图中的列表。但我使用了一个对象modelLogic。
public abstract class MainController implements IHandleSerialization {
Object obj = new Object();
private ModelLogic modelLogic = new ModelLogic();
public ModelLogic getModelLogic() {
return modelLogic;
}
MainPanelController:
public class MainPanelController extends MainController implements Initializable {
@FXML
private TopPanelController topPanelController;
public TopPanelController getTopPanelController() {
return topPanelController;
}
@FXML
private MenuPanelController menuPanelController;
@FXML
private LeftPanelTimeModeController leftPanelTimeModeController;
@FXML
private CenterPanelTableController centerPanelTableController;
@Override
public void initialize(URL location, ResourceBundle resources) {
getModelLogic().initDB();//if there is no database create it, otherwise not
updateComboBox();
showAddUserPanel();
showRemoveUserPanel();
setUser();
}