Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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.

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.

Here is the 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));
}

I appreciate the help, thank you!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
168 views
Welcome To Ask or Share your Answers For Others

1 Answer

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!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...