获取领域中的字段中的值的平均值

时间:2018-06-08 13:48:23

标签: android reactjs react-native realm

这告诉我'Can't find variable: gpa'

它也在说unresolved variable Double。我想获取在特定字段中提交的所有值并计算平均值。平均值将显示在警告框中。代码位于下面名为dbtotal的函数中。

class MainActivity extends 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'
                }
            }]
        });
    }

    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.");
    };

    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() {
    let cgpa: Double = realm.objects('CalcGP').avg(gpa) ;
    Alert.alert(cgpa); 
}

<TouchableOpacity onPress={this.dbtotal} activeOpacity={0.7} style={styles.button} >
     <Text style={styles.TextStyle}> CALCULATE </Text>
</TouchableOpacity>

2 个答案:

答案 0 :(得分:0)

avg(gpa)应为avg('gpa')

let cgpa: Double =可以是let cgpa =

答案 1 :(得分:0)

这很好用

`let average = realm.objects('CalcGP')。avg('gpa');

this.state = {       aver:Alert.alert(Your CGPA is + average.toString()) };`