realm - Realm 数据库中 FlatList 中的数据显示问题

标签 realm react-native react-native-ios react-native-flatlist

我已经将数据插入到 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/

相关文章:

javascript - 元素类型无效,您可能忘记导出组件

objective-c - react native : Pass JS variable to AppDelegate

swift - Cocoapods pod 文件安装无法正常工作 "Unable to find a specification for ` RealmSwift (~> 0.97 )`"swift

ios - 带有关系谓词的 Realm.io 快速查询

react-native - 如何在 react 导航 5.x 中创建透明背景?

javascript - React Native - 每秒多次触摸的 onTouchStart 与 PanResponder

ios - CocoaPods 找不到 pod 的兼容版本 "GoogleDataTransportCCTSupport"

php - 我应该使用 LightOpenID 将 REALM 设置为什么,以便 Google url 保持一致,以便存储在我的数据库中?

swift - Swift 中的内存 Realm 线程

reactjs - React Native 从另一个状态键访问 this.state.key