使用本地JSON React本机的部分列表

时间:2019-04-24 06:06:40

标签: react-native react-native-android react-native-ios

我有一个名为Observer.json的本地json文件

    {
      "Observer": {
       "Category_2":[{
         "row1" : "Reading_1",
         "row2" : "Reading_2",
         "row3" : "Reading_3",
         "row4" : "Reading_4"
       }]
      }
    }

我想在react native中使用部分列表显示此数据。将有一个标题为Category_2的部分和4行,即。 Reading_1等。请帮助我无法查看任何数据。

这是代码


    import React, { Component } from 'react';
    import { AppRegistry, View, Text,TouchableOpacity, ListView, StyleSheet,SectionList } from 'react-native';
    import { createStackNavigator,createAppContainer } from "react-navigation";

    const data_Custom = require('./Observer.json');

    class RenderList extends Component {

      static navigationOptions = {
        title: 'List Items'
      };

      constructor() {
      super();
      var ds = new ListView.DataSource({
        rowHasChanged: (r1, r2) => r1 !== r2,
        sectionHeaderHasChanged: (h1, h2) => h1 !== h2,
      });

      this.state = {
      dataSource: ds.cloneWithRowsAndSections({dataBlob: data_Custom}, {sectionIdentities:["Category_2"]}, {rowIdentities:["row1","row2","row3","row4"]} ),
      };
    }

     renderRow(custom){
      return (
       <View >
         <Text style = { styles.container2 }>
          {custom.rowIdentities}
         </Text>
       </View>
      );
     }

     renderSectionHeader(custom_s) {
      return (
        <Text style = { styles.container2 }>
         {custom_s.sectionIdentities}
        </Text>
      )
     }

     render() {
      return (
        <ListView
         dataSource={this.state.dataSource}
         renderRow={this.renderRow.bind(this)}
         renderSectionHeader={this.renderSectionHeader}
        />
      );
     }
    }

    const App = createStackNavigator({
      List: { screen: RenderList },
    });

    export default createAppContainer(App);

    const styles = StyleSheet.create({
      container1:{
       flex: 1
      },
      container2:{
       flex: 1,
       justifyContent: 'center',
       alignItems: 'center',
       paddingHorizontal: 15,
       fontSize: 18
      },
      item:{
       padding: 15
      },
      text:{
       fontSize: 18
      },
      separator:{
       height: 2,
       backgroundColor: 'rgba(0,0,0,0.5)'
      }
    });

1 个答案:

答案 0 :(得分:0)

您可以尝试以下吗?

首先按照以下步骤更改json文件,

 [
   {
      "title":"Observer",
      "data":[
         {"row" : "Reading_1"},
         {"row" : "Reading_2"},
         {"row" : "Reading_3"},
         {"row" : "Reading_4"}
       ]
    }
  ...
 ]

然后使用SectionList而不是ListView,因为ListView已过时。

import React, { Component } from 'react';
    import { AppRegistry, View, Text,TouchableOpacity, StyleSheet,SectionList } from 'react-native';
    import { createStackNavigator,createAppContainer } from "react-navigation";

    const data_Custom = require('./Observer.json');

    class RenderList extends Component {

     static navigationOptions = {
       title: 'List Items'
     };

     constructor() {
      super();
     this.state = {
      dataSource: data_Custom,
     };
    }

     renderRow(item){
      return (
       <View>
         <Text style = { styles.container2 }>
          {item.row}
         </Text>
       </View>
      );
     }

     renderSectionHeader(title) {
      return (
        <Text style = { styles.container2 }>
         {title}
        </Text>
      )
     }

     render() {
      return (
        <SectionList
         renderItem={({item, index, section}) => this.renderRow(item)}
         renderSectionHeader={({section: {title}}) => this.renderSectionHeader(title)}
         sections={this.state.dataSource}
         keyExtractor={(item, index) => item + index}
        />
      );
     }
    }

    const App = createStackNavigator({
      List: { screen: RenderList },
    });

    export default createAppContainer(App);

    const styles = StyleSheet.create({
      container1:{
       flex: 1
      },
      container2:{
       flex: 1,
       justifyContent: 'center',
       alignItems: 'center',
       paddingHorizontal: 15,
       fontSize: 18
      },
      item:{
       padding: 15
      },
      text:{
       fontSize: 18
      },
      separator:{
       height: 2,
       backgroundColor: 'rgba(0,0,0,0.5)'
      }
    });