`从'react'导入React,{Component};
从'react-native'导入{StyleSheet,Platform,View,Image,Text,TextInput,TouchableOpacity,Alert,YellowBox,ListView};
const Realm = require('realm');
让境界;
从'react-navigation'导入{StackNavigator};
类MainActivity扩展了Component {
static navigationOptions =
{
title: 'MyGPA',
};
GoToSecondActivity = () =>
{
this.props.navigation.navigate('Second');
};
constructor(){
super();
this.state = {
Student_Name : '',
Semester : '',
GPA : ''
};
realm = new Realm({
schema: [{name: 'CalcGP',
properties:
{
student_id: {type: 'int', default: 0},
student_name: 'string',
semester: 'int',
gpa: 'double'
}}]
});
YellowBox.ignoreWarnings([
'Warning: componentWillMount is deprecated',
'Warning: componentWillReceiveProps is deprecated',
'Warning: isMounted(...) is deprecated', 'Module RCTImageLoader'
]);
}
add_Student=()=>{
realm.write(() => {
let ID = realm.objects('CalcGP').length + 1;
realm.create('CalcGP', {
student_id: ID,
student_name: this.state.Student_Name,
semester: this.state.Semester,
gpa : this.state.GPA,
});
});
Alert.alert("Details Added Successfully.");
};
render() {
return (
<View style={styles.MainContainer}>
<TextInput
placeholder="Enter Student Name"
style = { styles.TextInputStyle }
underlineColorAndroid = "transparent"
onChangeText = { ( text ) => { this.setState({ Student_Name: text })} }
/>
<TextInput
placeholder="Enter Semester"
style = { styles.TextInputStyle }
underlineColorAndroid = "transparent"
onChangeText = { ( text ) => { this.setState({ Semester: text })} }
/>
<TextInput
placeholder="Enter GPA"
style = { styles.TextInputStyle }
underlineColorAndroid = "transparent"
onChangeText = { ( text ) => { this.setState({ GPA: text })} }
/>
<TouchableOpacity onPress={this.add_Student} activeOpacity={0.7} style={styles.button} >
<Text style={styles.TextStyle}> SAVE TO DATABASE </Text>
</TouchableOpacity>
<TouchableOpacity onPress={this.GoToSecondActivity} activeOpacity={0.7} style={styles.button} >
<Text style={styles.TextStyle}> SHOW MY GPA </Text>
</TouchableOpacity>
</View>
);
}
}
类ShowDataActivity扩展了Component { static navigationOptions = { 标题:'MyGPA', };
constructor() {
super();
this.state = {
Student_Name : '',
Semester : '',
GPA : ''
};
realm = new Realm({
schema: [{name: 'CalcGP',
properties:
{
student_id: {type: 'int', default: 0},
student_name: 'string',
semester: 'int',
gpa: 'double'
}}]
});
YellowBox.ignoreWarnings([
'Warning: componentWillMount is deprecated',
'Warning: componentWillReceiveProps is deprecated',
'Warning: isMounted(...) is deprecated', 'Module RCTImageLoader'
]);
let mydata = realm.objects('CalcGP');
let ds = new ListView.DataSource({rowHasChanged: (r1, r2) => r1 !== r2});
this.state = {
dataSource: ds.cloneWithRows(mydata),
};
}
GoToEditActivity (student_id, student_name, semester, gpa) {
this.props.navigation.navigate('Third', {
ID : student_id,
NAME : student_name,
CLASS : semester,
SUBJECT : gpa,
});
}
dbtotal() {
//allGP = realm.objects(GP);
let cgpa = realm.objects('CalcGP').avg(gpa) ;
Alert.alert(cgpa);
//let tanDogs = dogs.filtered('color = "tan" AND name BEGINSWITH "B"');
}
ListViewItemSeparator = () => {
return (
<View
style={{
height: .5,
width: "100%",
backgroundColor: "#000",
}}
/>
);
};
render()
{
return(
<View style = { styles.MainContainer }>
<ListView
dataSource={this.state.dataSource}
renderSeparator= {this.ListViewItemSeparator}
renderRow={(rowData) => <View style={{flex:1, flexDirection: 'column'}} >
<TouchableOpacity onPress={this.GoToEditActivity.bind(this, rowData.student_id, rowData.student_name, rowData.semester, rowData.gpa)} >
<Text style={styles.textViewContainer} >{'id = ' + rowData.student_id}</Text>
<Text style={styles.textViewContainer} >{'Name = ' + rowData.student_name}</Text>
<Text style={styles.textViewContainer} >{'Semester = ' + rowData.semester}</Text>
<Text style={styles.textViewContainer} >{'GPA = ' + rowData.gpa}</Text>
</TouchableOpacity>
</View> }
/>
<TouchableOpacity onPress={this.dbtotal} activeOpacity={0.7} style={styles.button} >
<Text style={styles.TextStyle}> CALCULATE </Text>
</TouchableOpacity>
</View>
);
}
}
类EditActivity扩展了Component {
static navigationOptions =
{
title: 'EditActivity',
};
constructor() {
super();
this.state = {
Student_Id : '',
Student_Name: '',
Semester: '',
GPA: ''
};
YellowBox.ignoreWarnings([
'Warning: componentWillMount is deprecated',
'Warning: componentWillReceiveProps is deprecated',
'Warning: isMounted(...) is deprecated', 'Module RCTImageLoader'
]);
}
componentDidMount(){
// Received Student Details Sent From Previous Activity and Set Into State.
this.setState({
Student_Id : this.props.navigation.state.params.ID,
Student_Name: this.props.navigation.state.params.NAME,
Semester: this.props.navigation.state.params.CLASS,
GPA: this.props.navigation.state.params.SUBJECT
})
}
Update_Student=()=>{
realm.write(() => {
const ID = this.state.Student_Id - 1;
const obj = realm.objects('CalcGP');
obj[ID].student_name = this.state.Student_Name;
obj[ID].semester = this.state.Semester;
obj[ID].gpa = this.state.GPA;
});
Alert.alert("GP Updated Successfully.")
};
Delete_Student=()=>{
realm.write(() => {
const ID = this.state.Student_Id - 1;
realm.delete(realm.objects('CalcGP')[ID]);
});
Alert.alert("Record Deleted Successfully.");
this.props.navigation.navigate('Show');
};
render() {
return (
<View style={styles.MainContainer}>
<TextInput
value={this.state.Student_Name}
style = { styles.TextInputStyle }
underlineColorAndroid = "transparent"
onChangeText = { ( text ) => { this.setState({ Student_Name: text })} }
/>
<TextInput
value={this.state.Semester}
style = { styles.TextInputStyle }
underlineColorAndroid = "transparent"
onChangeText = { ( text ) => { this.setState({ Semester: text })} }
/>
<TextInput
value={this.state.GPA}
style = { styles.TextInputStyle }
underlineColorAndroid = "transparent"
onChangeText = { ( text ) => { this.setState({ GPA: text })} }
/>
<TouchableOpacity onPress={this.Update_Student} activeOpacity={0.7} style={styles.button} >
<Text style={styles.TextStyle}> CLICK HERE TO UPDATE GP DETAILS </Text>
</TouchableOpacity>
<TouchableOpacity activeOpacity={0.7} style={styles.button} onPress={this.Delete_Student} >
<Text style={styles.TextStyle}> CLICK HERE TO DELETE CURRENT RECORD </Text>
</TouchableOpacity>
</View>
);
}
}
答案 0 :(得分:0)
使用这部分代码:
<TextInput
placeholder="Enter Semester"
style = { styles.TextInputStyle }
underlineColorAndroid = "transparent"
onChangeText = { ( text ) => { this.setState({ Semester: Number(text) })} }
/>