当前位置: 首页 > 知识库问答 >
问题:

CalcGP.sement必须为“number”类型,并具有“string”(“1”)

别宏盛
2023-03-14

`从“React”导入React,{Component};

从“react native”导入{样式表、平台、视图、图像、文本、TextInput、TouchableOpacity、Alert、YellowBox、ListView};

const Realm=require('Realm');

让王国;

从响应导航导入{StackNavigator};

扩展组件{

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扩展组件{static navigationOptions={title:'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>
    );
}

}

编辑活动类扩展组件{

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>

    );
}

}

共有1个答案

阮才俊
2023-03-14

使用这部分代码:

<TextInput
    placeholder="Enter Semester"
    style = { styles.TextInputStyle }
    underlineColorAndroid = "transparent"
    onChangeText = { ( text ) => { this.setState({ Semester: Number(text) })} }
/>
 类似资料:
  • 我正在学习Apollo和graphQL,并将其集成到我的一个项目中。到目前为止一切正常,但现在我正在尝试一些突变,我正在努力处理输入类型和查询类型。我觉得事情比实际情况要复杂得多,因此我正在寻求如何处理我的情况的建议。我在网上找到的示例都是非常基本的模式,但现实总是更加复杂,因为我的模式非常大,如下所示(我只复制一部分): 然后定义输入和负载,依此类推... 为此,我需要一个变异来保存“计算”,因

  • electron应用程序代码来自《跨平台桌面应用程序》一书,它旨在向浏览器显示用户文件目录,就像在桌面资源管理器应用程序中一样。 打印到开发人员工具控制台时出现的错误为“Uncaught(in promise)TypeError[ERR_INVALID_ARG_TYPE]:“path”参数必须是string类型。未定义接收。我做错了什么?

  • 问题内容: 这是我更新到后遇到的一个有趣的问题 错误就在网上 问题答案: 编译器告诉您不能使用,因为它完全没有必要。您没有要打开的任何可选选项:不是可选的,并且该属性也不是可选的。仅用于解开可选项。如果要创建一个名为的新常量,请执行以下操作: 但是,旁注:具有一个名为参数的参数和一个名为局部常量的参数会造成极大的混淆。这样可能会更好:

  • 关于ts as number 依旧是string 的问题 我这里代码已经在每一个步骤已经声明了是 number 但是依旧打印出来时是string 但是如果我使用 parseInt(sid) 又会提示我number 类型的参数不能赋值于 string 所以 类型转换是怎么转换的呢 ts 正确的类型转换

  • 问题内容: var firstName: String = “John Appleseed” if let name = firstName { print (“Hello, (name)”) } 第二行出现字符串错误:条件绑定的初始化程序必须具有可选类型,而不是’String’ 如何决定使用可选变量还是非可选变量? 问题答案: 首先,让我们考虑一下构造的含义。当你写 你告诉斯威夫特你想 尝试展开

  • 为了方便起见,我将setter方法返回类型更改为对象,例如: 但在tomcat做出这一改变之后 PropertyNotFoundException: 为了消除这个异常,我将修饰符从private更改为public,但仍然得到相同的错误。所以我有两个问题; null