从EditTexts平均 [英] Average from EditTexts

查看:233
本文介绍了从EditTexts平均的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的活动,有两个按钮:一个用于将项目添加到一个ListView,这是由一个EditText(其中,用户输入一个十进制数)形成,并且另外一个,这将开始计算。我们的目标是计算在EditTexts输入的数的平均值,这取决于加入到ListView项的数目

In my activity, there are two buttons: one for adding an item to a ListView, which is formed by an EditText (where the user enters a decimal number) and another one, which will start the calculation. The goal is to calculate the average of the numbers entered in the EditTexts, depending on the number of items added to the ListView.

我有以下的code,我想我可能会创建儿童的数量,然后除以孩子的加法,但我已经读了很多类似的例子,我不知道该怎么做

I have the following code, and I think I might get the number of childs created and then divide the adding by the childs, but I have read a lot of similar examples and I have no idea how to do that.

下面是code:

void addNumberFromText()
{
    double total=0;
    for(int i=0;i<MarkListView.getChildCount();i++)
    {
        View wantedView = MarkListView.getChildAt(i);
        markresult = (TextView)wantedView.findViewById(R.id.subjectmark);
        double value=Double.parseDouble(markresult.getText().toString());
        total+=value;

    }

    markresult1 = (TextView)findViewById(R.id.average);
    markresult1.setText(Double.toString(total));
    markresult1.setText(String.format("%.2f", total));
}

我AP preciate的帮助,谢谢!

I appreciate the help, thank you!

推荐答案

我有办法!我说:averagevalue =总/ MarkListView.getChildCount();这是儿童的数目

I have the solution! I added: averagevalue=total/MarkListView.getChildCount(); which is the number of childs.

void addNumberFromText()
{
    double total=0;
    double averagevalue=0;
    for(int i=0;i<MarkListView.getChildCount();i++)
    {
        View wantedView = MarkListView.getChildAt(i);
        markresult = (TextView)wantedView.findViewById(R.id.subjectmark);
        double value=Double.parseDouble(markresult.getText().toString());
        total+=value;
        averagevalue=total/MarkListView.getChildCount();

    }

    markresult1 = (TextView)findViewById(R.id.average);
    markresult1.setText(Double.toString(averagevalue));
    markresult1.setText(String.format("%.2f", averagevalue));

感谢您的答复,所有的相似和正确的。谢谢!

Thanks for the responses, all were similar and correct. Thank you!

这篇关于从EditTexts平均的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆