[英]Realm with React Native ListView will not render
在一个本机应用程序上工作,该应用程序记录用户在“内存”中行走的位置的坐标,并将内存(带有描述)和一组坐标存储在Realm中。
我在一个具有不同数据库的项目中使用了ListView,但是使用realm-listview我无法获取页面上呈现的内存的描述。
将我的领域设置存储在一个如下所示的文件中。 主要操作是将数据存储在“内存/坐标模式”中,并使用RealObjects.countMemories()函数获取要在ListView中使用的数据。 当我在控制台中查看时,它正确地返回了一个领域对象数组。
export default class RealmObjects {}
RealmObjects.MemorySchema = {
name: 'Memory',
properties: {
description: 'string',
coords: {type: 'list', objectType: 'Coordinate'},
}
}
RealmObjects.CoordinateSchema = {
name: 'Coordinate',
properties: {
longitude: 'double',
latitude: 'double',
timeStamp: 'double'
}
}
RealmObjects.saveMemory = function(coordinates, description) {
console.log('---------------realm write-------------')
console.log(description)
console.log(coordinates)
realm.write(() => {
let memory = realm.create('Memory', {
description: description,
id: 1
});
let coordList = memory.coords
for (var i = 0; i < coordinates.length; i++) {
console.log(i)
console.log(coordinates[i].timeStamp)
coordList.push({longitude: coordinates[i].longitude,
latitude: coordinates[i].latitude,
timeStamp: coordinates[i].timeStamp});
console.log(memory.coords[i].timeStamp)
}
});
}
RealmObjects.countMemories = function() {
console.log(realm.objects('Memory'));
return realm.objects('Memory');
}
import Realm from 'realm';
let realm = new Realm({schema: [RealmObjects.MemorySchema, RealmObjects.CoordinateSchema]});
在我的组件中,我要像这样import { ListView } from 'realm/react-native';
ListView组件: import { ListView } from 'realm/react-native';
。
构造器设置如下:
constructor() {
super();
const ds = new ListView.DataSource({rowHasChanged: (r1, r2) => r1 !== r2});
this.state = {
dataSource: ds.cloneWithRows(RealmObjects.countMemories()),
};
console.log('datasource')
console.log(this.state.dataSource)
}
渲染功能设置如下:
render() {
return (
<View style={styles.container}>
<TouchableHighlight style={styles.buttonLeft} onPress={() => this.goBack()}>
<Text>Back</Text>
</TouchableHighlight>
<ListView style={styles.listview}
dataSource={this.state.dataSource}
renderRow={(rowData) => <Text>something: {rowData.description}</Text>}
/>
</View>
);
}
此渲染方法不会在ListView中产生任何结果。 现在,我只是想获取要在屏幕上显示的内存描述列表。
对于本机和领域都是新手,因此不确定我的问题是否在这里或其他地方。 任何帮助表示赞赏!
编辑:更改了渲染行和行数据项,使其看起来像这样记录数据,该数据记录了控制台中每行的正确描述,但在屏幕上仍然不显示任何内容:
renderRow(rowData) {
console.log('my data');
console.log(rowData.description);
return (
<Text>{rowData.description}</Text>
);
}
render() {
return (
<View style={styles.container}>
<TouchableHighlight style={styles.buttonLeft} onPress={() => this.goBack()}>
<Text>Back</Text>
</TouchableHighlight>
<ListView style={styles.listview}
dataSource={this.state.dataSource}
renderRow={(rowData) => this.renderRow(rowData)}
/>
</View>
);
}
编辑:当前样式:
const styles = StyleSheet.create({
button: {
paddingHorizontal: 12,
alignItems: 'center',
marginHorizontal: 10,
top: 50,
backgroundColor: 'rgba(236,64,122,0.7)',
paddingVertical: 12,
},
listview: {
top: 100,
backgroundColor: 'rgba(236,64,122,0.7)',
},
container: {
flex: 1,
flexDirection: 'column',
marginTop: 60,
},
buttonRight: {
position: 'absolute',
top: 0,
right: 5,
backgroundColor: 'rgba(236,64,122,0.7)',
paddingHorizontal: 18,
paddingVertical: 12,
borderRadius: 20,
},
buttonLeft: {
position: 'absolute',
top: 0,
left: 5,
backgroundColor: 'rgba(236,64,122,0.7)',
paddingHorizontal: 18,
paddingVertical: 12,
borderRadius: 20,
}
});
我也曾经遇到过同样的问题。 但是我将来自领域的响应转换为数组,然后将其传递到平面列表,它的工作就像一个魅力。
async getAllPrinterArray() {
realm = await this.getRealMObject();
favoritePrinterArray = realm.objects(constants.SCHEMA_SAVE_PRINTERS);
console.log(favoritePrinterArray);
if (favoritePrinterArray.length > 0) {
this.setState({ enable: false });
arr4 = Array.from(favoritePrinterArray);
}
return arr4;
}
<FlatList
data={printerObject}
numColumns={2}
renderItem={({ item }) => this.renderListRow(item)}
/>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.