我是React Native的新手,我正在React Native博览会中制作一个ViewDepartment屏幕,用户可以在其中查看从数据库获取的部门。要获取的值,我想以某种表格式显示,为此我正在使用平面列表,但在renderRow中存在问题。
这是我的使用平面列表的ViewDept类:
export default class ViewDept extends Component{
constructor(props){
super(props)
this.state={
// dataSource: null,
isLoading: true,
departments:[
[]
]
};
}
componentDidMount() {
/*let currentComponent = this;
deptref.on("value", function(snapshot) {
console.log(snapshot.val());
let data = snapshot.val();
let departments = Object.values(data);
currentComponent.setState({departments});
});*/
this.listenForItems();
}
listenForItems() {
deptref.on("value", function(snapshot) {
console.log(snapshot.val());
let data = snapshot.val();
let departments = Object.values(data);
this.departments=departments;
let ds = new ListView.DataSource({rowHasChanged: (r1, r2) => r1 !== r2});
//dataSource: ds.cloneWithRows(departments);
});
}
GetItem (name) {
Alert.alert(name);
}
ListViewItemSeparator = () => {
return (
<View
style={{
height: .5,
width: "100%",
backgroundColor: "#000",
}}
/>
);
}
render() {
/* if (this.state.isLoading) {
return (
<View style={{flex: 1, paddingTop: 20}}>
<ActivityIndicator />
</View>
);
} else{*/
return (
<View style={styles.MainContainer}>
<ListView
dataSource={this.state.dataSource}
renderSeparator= {this.ListViewItemSeparator}
renderRow={(rowData) =>
<View style={{flex:1, flexDirection: 'column'}} >
// <TouchableOpacity onPress={this.GetItem.bind(this, rowData.data.department)} >
<Text style={styles.textViewContainer} >{'DEpartments = ' + rowData.data.department}</Text>
<Text style={styles.textViewContainer} >{'Description= ' + rowData.data.description}</Text>
<Text style={styles.textViewContainer} >{'Name = ' + rowData.data.name}</Text>
<Text style={styles.textViewContainer} >{'Purpose = ' + rowData.data.purpose}</Text>
// </TouchableOpacity>
</View> }
/>
</View>
);
// }
}
}