我已经将数据插入到 Realm 数据库中。
现在,将数据插入到数据库后,我从数据库中检索数据并尝试将其显示在react-native中的FlatList中。
但是,它在列表中显示未定义..并且 FlatList 中的许多行的值为“未定义”。
下面是我的组件的 render() 方法:
render() {
var A = realm.objects('Student_Info');
var myJSON = JSON.stringify(A);
return (
<View style={styles.MainContainer}>
<TextInput
placeholder="Student Name"
style = { styles.TextInputStyle }
underlineColorAndroid = "transparent"
onChangeText = { ( text ) => { this.setState({ Student_Name: text })} }
/>
<TextInput
placeholder="Class"
style = { styles.TextInputStyle }
underlineColorAndroid = "transparent"
onChangeText = { ( text ) => { this.setState({ Student_Class: text })} }
/>
<TextInput
placeholder="Subject"
style = { styles.TextInputStyle }
underlineColorAndroid = "transparent"
onChangeText = { ( text ) => { this.setState({ Student_Subject: text })} }
/>
<TouchableOpacity onPress={this.add_Student} activeOpacity={0.7} style={styles.button} >
<Text style={styles.TextStyle}> Make Student Entry </Text>
</TouchableOpacity>
<Text style={{marginTop: 10}}>{myJSON}</Text>
<FlatList
//data={this.state.dataSource}
data={myJSON}
ItemSeparatorComponent={this._flatListItemSeparator}
renderItem={({ item }) =>
<View style={{ flex: 1, flexDirection: 'row' }} >
<Text style={styles.textView}>{"Student Name : "+item.student_id}</Text>
</View>
}
keyExtractor={(item, index) => index.toString()}
/>
</View>
);
可能是什么问题?
谢谢。
编辑
我从数据库获取的 json 数据如下:
{"0":{"student_id":1,"student_name":"Ashish","student_class":"React","student_subject":"React native"},"1":{"student_id":2,"student_name":"Ashish1","student_class":"React1","student_subject":"React native1"},"2":{"student_id":3,"student_name":"","student_class":"","student_subject":""},"3":{"student_id":4,"student_name":"","student_class":"","student_subject":""}}
架构已使用:
realm = new Realm({
schema: [{
name: 'Student_Info',
properties:
{
student_id: { type: 'int', default: 0 },
student_name: 'string',
student_class: 'string',
student_subject: 'string'
}
}]
});
最佳答案
您错过的一个关键事情是,flatlist data
属性应该是一个数组。所以我更新了你的 JSON 就像
let myJSON = [
{
"0": {
student_id: 1,
student_name: "Ashish",
student_class: "React",
student_subject: "React native"
},
"1": {
student_id: 2,
student_name: "Ashish1",
student_class: "React1",
student_subject: "React native1"
},
"2": {
student_id: 3,
student_name: "",
student_class: "",
student_subject: ""
},
"3": {
student_id: 4,
student_name: "",
student_class: "",
student_subject: ""
}
}
];
并使用Object.values(myJSON)
获取数据对象数组。使用如下所示的平面列表
<View>
<FlatList
//data={this.state.dataSource}
data={myJSON}
renderItem={({ item }) => (
<View style={{ flex: 1, flexDirection: "column" }}>
<Text style={styles.textView}>{item[0].student_id}</Text>
<Text style={styles.textView}>{item[1].student_id}</Text>
<Text style={styles.textView}>{item[2].student_id}</Text>
<Text style={styles.textView}>{item[3].student_id}</Text>
</View>
)}
keyExtractor={(item, index) => index.toString()}
/>
</View>
关于realm - Realm 数据库中 FlatList 中的数据显示问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51606604/