我正在尝试在Listview中显示数据。首先,我根据用户在上一个屏幕上做出的选择来过滤数据(JSON文件)。我通过说:
将参数传递到下一个屏幕this.props.navigation.state.params.JSON_ListView_Clicked_Item)
基于上面的参数,我在render函数中做了ListView.DataSource。唯一的原因,我试图在render函数中做ListView.DatSource,因为据我所知这是我可以读取参数值的唯一地方。当我试图显示我的listView时,我收到一条错误说:
TypeError undefined不是对象。以下是截图:
以下是我的代码:
import React, { Component } from 'react';
import { StyleSheet, Text, View, ListView, ActivityIndicator, TextInput } from 'react-native';
import ServiceDetails from '../reducers/ServiceDetails';
class ServiceListDetails extends Component
{
static navigationOptions =
{
title: 'SecondActivity',
};
ListViewItemSeparator = () => {
return (
<View
style={{
height: .5,
width: "100%",
backgroundColor: "#000",
}}
/>
);
}
render()
{
var x = this.props.navigation.state.params.JSON_ListView_Clicked_Item ;
var newList = ServiceDetails.filter(obj => obj.fk === this.props.navigation.state.params.JSON_ListView_Clicked_Item)
let ds = new ListView.DataSource({rowHasChanged: (r1, r2) => r1 !== r2});
this.setState({
dataSource: ds.cloneWithRows(newList)
}
);
return(
<View style={styles.MainContainer}>
<ListView
dataSource={this.dataSource}
renderSeparator= {this.ListViewItemSeparator}
renderRow={(rowData) => <Text>{rowData}</Text>}
/>
</View>
);
}
}
以下是我的JSON文件:
[
{
"id":"1",
"fk": "1",
"addr": "TestAddress1",
"phone": "(888)889-9999",
"LatL":"33.234567",
"Long2":"117.284725",
"Online": "x"
},
{
"id":"2",
"fk": "1",
"addr": "TestAddress1",
"phone": "(999)-999-9999",
"LatL":"33.971110",
"Long2":"117.31111",
"Online": ""
},
]
答案 0 :(得分:0)
定义JSON文件时出错。在数组","
中的最后一个对象中,不需要它是一个有效的JSON格式。
[
{
"id": "1",
"fk": "1",
"addr": "TestAddress1",
"phone": "(888)889-9999",
"LatL": "33.234567",
"Long2": "117.284725",
"Online": "x"
},{
"id": "2",
"fk": "1",
"addr": "TestAddress1",
"phone": "(999)-999-9999",
"LatL": "33.971110",
"Long2": "117.31111",
"Online": "y"
}
]