我有一个表单,可以在其中添加一些数据并将其存储在数据库中,但是我只能添加一次,因此我必须返回到上一个屏幕,然后再返回输入数据的屏幕。请在整个项目中帮助我解决这个问题。
在我的项目中,我有三个选项,当我单击它时,下面的屏幕将打开,添加一些数据并将其成功存储在数据库中,但其中之一是“标记分布”,但是当我尝试添加新数据时会出现错误表示“更新条目时发生错误。有关详细信息,请参阅内部异常。”。当我回头再来一次时,它成功保存了。
下面是我的代码
import React, { Component } from 'react';
import {
StyleSheet,
View,
Text,
TouchableOpacity,
Image,
ImageBackground,
FlatList
} from 'react-native';
import { AsyncStorage } from 'react-native';
import Dialog from "react-native-dialog";
import { Dropdown } from 'react-native-material-dropdown';
import { TextInput } from 'react-native-gesture-handler';
export default class assignments extends React.Component {
static navigationOptions = {
title: '',
headerStyle: {
backgroundColor: 'rgba(0,155,35,0.8)',
},
headerTintColor: '#fff',
headerTitleStyle: {
fontWeight: 'bold',
},
};
constructor(props) {
super(props);
this.state = {
assignmentArray: [],
refreshing: false,
cloNoTxt: '',
assignmentNoTxt: '',
}
}
componentDidMount = async () => {
let coursetitle = await AsyncStorage.getItem('couresTitle');
this.setState({ title: JSON.parse(coursetitle) });
let cTeacher = await AsyncStorage.getItem('couresTeacher');
this.setState({ teacher: JSON.parse(cTeacher) });
this.getAssignmentsData();
return fetch('http://10.211.55.6/OBEServices/api/getClos/' + this.state.title + '')
.then((response) => response.json())
.then((responseJson) => {
let cloNoData = [];
for (var i = 0; i < responseJson.length; i++) {
cloNoData.push({ value: responseJson[i].CloNo });
}
this.setState({ cloNoData });
})
}
getAssignmentsData = async () => {
return fetch('http://10.211.55.6/OBEServices/api/getAssignments/' + this.state.title + '/' + this.state.teacher + '')
.then((response) => response.json())
.then((responseJson) => {
this.setState({
isLoading: false,
closDataSource: responseJson,
})
})
}
handelRefresh() {
this.setState({
refreshing: true
}, () => {
this.componentDidMount()
this.setState({ quizNoTxt: '' })
this.setState({ tMarksTxt: '' })
this.setState({ cloNoTxt: '' })
this.setState({ refreshing: false })
})
}
addAssignmentsData = () => {
if (this.state.tMarksTxt != null) {
this.state.assignmentArray.push({
'Subject': this.state.title.toString(),
'Teacher': this.state.teacher.toString(),
'AssignmentNo': this.state.assignmentNoTxt,
'TotalMarks': this.state.tMarksTxt,
'CLONo': this.state.cloNoTxt,
});
//console.warn(this.state.assignmentArray)
fetch('http://10.211.55.6/OBEServices/api/insertAssignment', {
method: 'POST',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json'
}, body: JSON.stringify(this.state.assignmentArray[0])
}).then((Response) => Response.json()).then((responseData) => {
alert(responseData)
})
this.handelRefresh()
}
else {
alert("Fields Empty")
}
}
assignmentNoChangeTextHandler(assignmentNo) {
this.setState({ assignmentNoTxt: assignmentNo })
}
cloNoChangeTextHandler(cloNo) {
this.setState({ cloNoTxt: cloNo })
}
render() {
let data = [
{ value: "A1" },
{ value: "A2" },
{ value: "A3" },
{ value: "A4" },
{ value: "A5" },
{ value: "A6" },
{ value: "A7" },
{ value: "A8" },
{ value: "A9" },
]
return (
<ImageBackground
source={require('../../../../Images/Backgrounds/bg3.jpeg')}
style={styles.backgroundContainer}>
<View style={styles.backgroundContainer}>
<Text style={{ fontSize: 20, fontWeight: "bold", marginTop: 50, marginBottom: 20, color: 'rgba(0, 0, 0, 0.8)', }}>ASSIGNMENT</Text>
<View style={styles.rowStyle}>
<View style={styles.columnStyle}>
<Text>Assignment No</Text>
</View>
<View style={styles.columnStyle}>
<Text>T Marks</Text>
</View>
<View style={styles.columnStyle}>
<Text>Clo No</Text>
</View>
</View>
<View style={styles.rowStyle}>
<View style={styles.dropdownStyle}>
<Dropdown
placeholder="Choose"
placeholderTextColor='rgba(0,0,0,0.4)'
data={data}
onChangeText={assignmentNo => this.assignmentNoChangeTextHandler(assignmentNo)}
/>
</View>
<View style={styles.inputStyle}>
<TextInput
style={styles.input}
keyboardType={'numeric'}
placeholderTextColor={'rgba(0, 0, 0, 0.4)'}
underlineColorAndroid='transparent'
autoCapitalize="none"
onChangeText={tMarksTxt => this.setState({ tMarksTxt })} />
</View>
<View style={styles.dropdownStyle}>
<Dropdown
placeholder="Choose"
placeholderTextColor='rgba(0,0,0,0.4)'
data={this.state.cloNoData}
onChangeText={cloNo => this.cloNoChangeTextHandler(cloNo)}
/>
</View>
</View>
<TouchableOpacity
style={styles.btnSubmit}
onPress={this.addAssignmentsData}>
<Text style={styles.text}>SUBMIT</Text>
</TouchableOpacity>
</View>
<View style={styles.rowStyle}>
<View style={styles.columnStyle}>
<Text style={{fontWeight: "bold"}}>Assignment No</Text>
</View>
<View style={styles.columnStyle}>
<Text style={{fontWeight: "bold"}}>T Marks</Text>
</View>
<View style={styles.columnStyle}>
<Text style={{fontWeight: "bold"}}>Clo No</Text>
</View>
</View>
<FlatList
style={{ width: '100%' }}
data={this.state.closDataSource}
onRefresh={() => this.handelRefresh()}
refreshing={this.state.refreshing}
renderItem={({ item }) =>
<View style={styles.rowStyle}>
<View style={styles.columnStyle}>
<Text style={styles.courseTitleText}>{item.AssignmentNo}</Text>
</View>
<View style={styles.columnStyle}>
<Text style={styles.creditHrsText}>{item.TotalMarks}</Text>
</View>
<View style={styles.columnStyle}>
<Text style={styles.creditHrsText}>{item.CLONo}</Text>
</View>
</View>}
keyExtractor={(item, index) => index.toString()}>
</FlatList>
</ImageBackground>
);
}
}
const styles = StyleSheet.create({
backgroundContainer: {
flex: 1,
width: '100%',
height: '100%',
resizeMode: 'stretch',
//justifyContent: "center",
alignItems: "center",
},
input: {
height: 35,
width: '100%',
borderColor: 'black',
fontSize: 14,
marginTop: 15,
paddingLeft: 10,
},
inputView: {
width: 110,
justifyContent: "center",
alignItems: "center",
flexDirection: 'row',
paddingBottom: 5,
},
textStyle: {
paddingRight: 20,
fontSize: 16,
fontWeight: "bold",
color: 'rgba(0, 0, 0, 0.7)',
},
btnSubmit: {
width: 170,
height: 50,
backgroundColor: 'rgba(0,155,35,0.8)',
justifyContent: "center",
alignItems: "center",
marginTop: 30,
marginBottom: 40,
borderRadius: 5
},
columnStyle: {
width: '33.33%',
height: 30,
borderWidth: 1,
justifyContent: "center",
alignItems: "center",
},
rowStyle: {
width: '100%',
flexDirection: "row",
},
dropdownStyle: {
width: '33.33%',
height: 55,
borderBottomWidth: 1,
paddingLeft: 10,
marginTop: -15,
justifyContent: "center",
},
inputStyle: {
width: '33.33%',
height: 55,
borderBottomWidth: 1,
borderLeftWidth: 1,
borderRightWidth: 1,
paddingLeft: 10,
marginTop: -15,
justifyContent: "center",
}
});