Android 领域数据更改时更新ListVIew中的数据
我在RN应用程序中使用Realm,其设置如下:Android 领域数据更改时更新ListVIew中的数据,android,listview,react-native,realm,Android,Listview,React Native,Realm,我在RN应用程序中使用Realm,其设置如下: this.classes = realm.objects('Notebook').sorted('active'); this.classes.addListener((name, changes) => { console.log("changed: " + JSON.stringify(changes)); console.log("Number of items: "
this.classes = realm.objects('Notebook').sorted('active');
this.classes.addListener((name, changes) => {
console.log("changed: " + JSON.stringify(changes));
console.log("Number of items: " + this.classes.length);
});
var dataSource = new ListView.DataSource(
{rowHasChanged: (r1, r2) => r1.name !== r2.name});
this.state = {
dataSource: dataSource.cloneWithRows(this.classes),
src: dataSource
};
起初一切都很好。插入新对象时,如下所示:
realm.write(() => {
realm.create('Notebook', {
...
});
});
我在我的侦听器中得到通知,某些内容已更改。然而,我看到的是:
更改:{“删除”:[]、“插入”:[]、“修改”:[]
项目数目:8
我认为应该在“插入”中列出一些内容。此外,项目的数量始终是添加新数据之前存在的数量。有人能告诉我是否设置错误吗?有什么理由不使用flatlist吗?通过将
类
直接传递到数据源,无需添加侦听器。我需要一个侦听器,以便知道何时添加/删除/更新类,并相应地更新我的ListView。