可观察清单

fr3*_*zex 4 java constructor javafx observablelist

当试图宣布一个新的ObservableList:

ObservableList<Account> userAccounts = new FXCollections.observableArrayList();

我收到一个错误observableArrayList();,上面写着:

找不到符号,符号:class observableArrayList,location:class FXCollections.

这是我的import语句

import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
Run Code Online (Sandbox Code Playgroud)

这是我的方法

public ObservableList<Account> getUsersAccounts(int memberID) { 
    ObservableList<Account> userAccounts = new FXCollections.observableArrayList();

    try {     
        Statement stmt = conn.createStatement();            
        String sql = "SELECT * FROM account WHERE member_id='" + memberID + "'";            
        ResultSet rs = stmt.executeQuery(sql);

        while(rs.next()) {
            Account account = new Account(rs.getInt("member_id"), rs.getString("account_type"), rs.getDouble("balance"));
            userAccounts.add(account);
        }
    } catch (SQLException ex) {
        Logger.getLogger(JDBCManager.class.getName()).log(Level.SEVERE, null, ex);
    }

    return userAccounts;
}
Run Code Online (Sandbox Code Playgroud)

我错过了什么,为什么我不能申报新的ObservableList

And*_*lko 6

可以使用构造函数直接创建实例,也可以通过调用可以调用此构造函数的方法隐式创建实例.

在你的情况下,它是一个静态方法.看看这些技巧:

List<String> a = new ArrayList<>();
List<String> b = Lists.createList();

class Lists {
    public static <T> List<T> createList() {
        return new ArrayList<>();
    }
}
Run Code Online (Sandbox Code Playgroud)


par*_*rsa 6

更改

ObservableList<Account> userAccounts = new FXCollections.observableArrayList();
Run Code Online (Sandbox Code Playgroud)

ObservableList<Account> userAccounts = FXCollections.observableArrayList();
Run Code Online (Sandbox Code Playgroud)