嗨,我不能在数据库中添加多个用户。我想添加arrayLits中的所有用户。我的代码是:
ArrayList < User > u = new ArrayList < > ();
User user1 = new User("hely", "rrr", "wdssa", "2");
User user2 = new User("jess", "rrr", "sds", "2");
User user3 = new User("mark", "merinos", "dfgrh", "2");
u.add(user1);
u.add(user2);
u.add(user3);
ArrayList < User > user = u;
new Thread(new Runnable() {
@ Override
public void run() {
Model_user.getInstance(getApplicationContext()).addUsers(user.toArray(new User[0]));
Log.d("main_activity_user", "ok user add");
}
}).start();
和我的模型是:
public class Model_user {
private static Model_user theinstance = null;
private ArrayList<User> user_list;
private static AppDatabase userDAOs = null;
public static synchronized Model_user getInstance() {
if (theinstance == null) {
theinstance = new Model_user();
}
return theinstance;
}
public static synchronized Model_user getInstance(Context c) {
if (theinstance == null) {
userDAOs = Room.databaseBuilder(c, AppDatabase.class, "userdb").build();
theinstance = new Model_user();
}
return theinstance;
}
private Model_user() {
user_list = new ArrayList<User>();
}
public void addUsers(User[] s) {
Log.d("main_activity_uid", "ok sta partendo");
userDAOs.userDAO().insertAll(s);
user_list.addAll(Arrays.asList(s));
Log.d("main_activity_uid", "ok hai inserito crrettamente");
return;
}
所以我只将第一个用户添加到数据库中,而不添加其他用户。如何在数据库中添加所有用户?
删除新用户[0];从adduser(用户。toArray(新用户[0])
应该是addUsers(user.toArray())