如何在android数据库中存储java列表
如何在android数据库中存储java列表。我试图使用模型中的setter方法来存储它,但它不起作用,我在异常消息中得到“value”的每个元素都必须是有效的托管对象”如何在android数据库中存储java列表,android,realm,realm-mobile-platform,Android,Realm,Realm Mobile Platform,如何在android数据库中存储java列表。我试图使用模型中的setter方法来存储它,但它不起作用,我在异常消息中得到“value”的每个元素都必须是有效的托管对象” public void storeNewsList(String categoryId, List<News> newsList) { Realm realm = Realm.getDefaultInstance(); realm.beginTransaction(); NewsLis
public void storeNewsList(String categoryId, List<News> newsList) {
Realm realm = Realm.getDefaultInstance();
realm.beginTransaction();
NewsList newsListObj = realm.createObject(NewsList.class);
newsListObj.setNewsList(new RealmList<>(newsList.toArray(new News[newsList.size()])));
newsListObj.setCategoryId(categoryId);
realm.commitTransaction();
realm.close();
}
public void storeNewsList(字符串categoryId,List newsList){
Realm Realm=Realm.getDefaultInstance();
realm.beginTransaction();
NewsList newsListObj=realm.createObject(NewsList.class);
newsListObj.setNewsList(newrealmlist(newsList.toArray)(newnews[newsList.size()));
newsListObj.setCategoryId(categoryId);
realm.commitTransaction();
realm.close();
}
将代码替换为
public void storeNewsList(String categoryId, List<News> newsList) {
try(Realm realm = Realm.getDefaultInstance()) {
realm.executeTransaction(new Realm.Transaction() {
@Override
public void execute(Realm realm) {
NewsList newsListObj = new NewsList(); // <-- create unmanaged
RealmList<News> _newsList = new RealmList<>();
_newsList.addAll(newsList);
newsListObj.setNewsList(_newsList);
newsListObj.setCategoryId(categoryId);
realm.insert(newsListObj); // <-- insert unmanaged to Realm
}
});
}
}
public void storeNewsList(字符串categoryId,List newsList){
试试(Realm Realm=Realm.getDefaultInstance()){
realm.executeTransaction(新realm.Transaction(){
@凌驾
公共void执行(领域){
NewsList newsListObj=new NewsList();//如果您正在使用@PrimaryKey,那么insertOrUpdate
就可以了
try(Realm realm = Realm.getDefaultInstance()) {
realm.executeTransaction(new Realm.Transaction() {
@Override
public void execute(Realm realm) {
RealmList<News> _newsList = new RealmList<>();
_newsList.addAll(myCustomArrayList);
realm.insertOrUpdate(_newsList); // <-- insert unmanaged to Realm
}
});
}
try(Realm-Realm=Realm.getDefaultInstance()){
realm.executeTransaction(新realm.Transaction(){
@凌驾
公共void执行(领域){
RealmList _newsList=新的RealmList();
_addAll(myCustomArrayList);
realm.insertOrUpdate(_newsList);//分享你的代码你做了什么?public void storeNewsList(String categoryId,List newsList){realm realm=realm.getDefaultInstance();realm.beginTransaction();newsList newsListObj=realm.createObject(newsList.class);newsListObj.setNewsList(newsList.toArray(News[newsList.size()););newsListObj.setCategoryId(categoryId);realm.commitTransaction();realm.close();}newsListObj.setNewsList(News RealmList));获取错误无法解析构造函数。啊,那么addAll()
RealmList改为newsList=new RealmList();我需要添加RealmList\U newsList=new RealmList();当然,可以尝试使用资源instance@Aks4125天哪,不,你每行泄漏2个领域实例