title: Calculate average from EditTexts
body: <p>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.</p>
  
  <p>Here is the code of the Activity:</p>
  
  <pre><code>public class PersActivity extends Activity {
  
  Button start, calcaverage1;
  ListView MarkListView;
  TextView markresult;
  private SubjectAdapter adapter;
  
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.subject_list_view);
  
      setupListViewAdapter();
  
      setupAddMarkButton();
  
      // Start button
              start = (Button)findViewById(R.id.start1);
              start.setOnClickListener(new OnClickListener() 
              {   public void onClick(View v) 
                  {   
                      Intent intent = new Intent(PersActivity.this, MainActivity.class);
                          startActivity(intent);      
                          finish();
                  }
              });
  
      // Calc average button
              calcaverage1 = (Button) findViewById(R.id.calcaverage);
              calcaverage1.setOnClickListener(new OnClickListener() {
  
                  @Override
                  public void onClick(View v) {
                      addNumberFromText();                        
                  }
              });
  }
  
  public void removeClick(View v) {
      Mark itemToRemove = (Mark)v.getTag();
      adapter.remove(itemToRemove);
  }
  
  private void setupListViewAdapter() {
      adapter = new SubjectAdapter(PersActivity.this, R.layout.subject_list_item, new ArrayList&lt;Mark&gt;());
      MarkListView = (ListView)findViewById(R.id.subject_list_item);
      MarkListView.setAdapter(adapter);
  }
  
  private void setupAddMarkButton() {
      findViewById(R.id.addsubject).setOnClickListener(new OnClickListener() {
  
          @Override
          public void onClick(View v) {
              adapter.insert(new Mark("", 0), 0);
          }
      });
  }
  void addNumberFromText()
  {
      double total=0;
      for(int i=0;i&lt;MarkListView.getChildCount();i++)
      {
          View wantedView = MarkListView.getChildAt(i);
          markresult = (TextView)wantedView.findViewById(R.id.subjectmark);
          double value=Double.parseDouble(markresult.toString());
          total+=value;
      }
      Log.d("PersActivity","total sum is "+total);
  }
  
  }
  </code></pre>
  
  <p>And here is the code of the adapter I used:</p>
  
  <pre><code>public class SubjectAdapter extends ArrayAdapter&lt;Mark&gt; {
  
  protected static final String LOG_TAG = SubjectAdapter.class.getSimpleName();
  
  private List&lt;Mark&gt; items;
  private int layoutResourceId;
  private Context context;
  
  public SubjectAdapter(Context context, int layoutResourceId, List&lt;Mark&gt; items) {
      super(context, layoutResourceId, items);
      this.layoutResourceId = layoutResourceId;
      this.context = context;
      this.items = items;
  }
  
  @Override
  public View getView(int position, View convertView, ViewGroup parent) {
      View row = convertView;
      MarkHolder holder = null;
  
      LayoutInflater inflater = ((Activity) context).getLayoutInflater();
      row = inflater.inflate(layoutResourceId, parent, false);
  
      holder = new MarkHolder();
      holder.Mark = items.get(position);
      holder.removeButton = (ImageButton)row.findViewById(R.id.remove);
      holder.removeButton.setTag(holder.Mark);
  
      holder.name = (TextView)row.findViewById(R.id.subjectname);
      setNameTextChangeListener(holder);
      holder.value = (TextView)row.findViewById(R.id.subjectmark);
      setValueTextListeners(holder);
  
      row.setTag(holder);
  
      setupItem(holder);
      return row;
  }
  
  private void setupItem(MarkHolder holder) {
      holder.name.setText(holder.Mark.getName());
      holder.value.setText(String.valueOf(holder.Mark.getValue()));
  }
  
  public static class MarkHolder {
      Mark Mark;
      TextView name;
      TextView value;
      ImageButton removeButton;
  }
  
  private void setNameTextChangeListener(final MarkHolder holder) {
      holder.name.addTextChangedListener(new TextWatcher() {
  
          @Override
          public void onTextChanged(CharSequence s, int start, int before, int count) {
              holder.Mark.setName(s.toString());
          }
  
          @Override
          public void beforeTextChanged(CharSequence s, int start, int count, int after) { }
  
          @Override
          public void afterTextChanged(Editable s) { }
      });
  }
  
  private void setValueTextListeners(final MarkHolder holder) {
      holder.value.addTextChangedListener(new TextWatcher() {
  
          @Override
          public void onTextChanged(CharSequence s, int start, int before, int count) {
              try{
                  holder.Mark.setValue(Double.parseDouble(s.toString()));
              }catch (NumberFormatException e) {
                  Log.e(LOG_TAG, "error reading double value: " + s.toString());
              }
          }
  
          @Override
          public void beforeTextChanged(CharSequence s, int start, int count, int after) { }
  
          @Override
          public void afterTextChanged(Editable s) { }
      });
  }
  }
  </code></pre>
  
  <p>The result will be displayed via TextView after pressing the button.</p>
  
  <p>Here is a screenshot of the activity. Actually it has no errors, but the app stops when I try to start the activity.</p>
  
  <p><img src="https://i.stack.imgur.com/QyVfL.png" alt="enter image description here"></p>
  
  <p><strong>EDIT3:</strong> LogCat error when pressing calculateaverage button</p>
  
  <pre><code>08-21 15:59:34.170: E/AndroidRuntime(1678): FATAL EXCEPTION: main
  08-21 15:59:34.170: E/AndroidRuntime(1678): java.lang.NumberFormatException: Invalid double: "android.widget.EditText@536f3e10"
  08-21 15:59:34.170: E/AndroidRuntime(1678):     at java.lang.StringToReal.invalidReal(StringToReal.java:63)
  08-21 15:59:34.170: E/AndroidRuntime(1678):     at java.lang.StringToReal.initialParse(StringToReal.java:114)
  08-21 15:59:34.170: E/AndroidRuntime(1678):     at java.lang.StringToReal.parseDouble(StringToReal.java:263)
  08-21 15:59:34.170: E/AndroidRuntime(1678):     at java.lang.Double.parseDouble(Double.java:295)
  08-21 15:59:34.170: E/AndroidRuntime(1678):     at com.isa.gestordecurso.PersActivity.addNumberFromText(PersActivity.java:81)
  08-21 15:59:34.170: E/AndroidRuntime(1678):     at com.isa.gestordecurso.PersActivity$2.onClick(PersActivity.java:49)
  08-21 15:59:34.170: E/AndroidRuntime(1678):     at android.view.View.performClick(View.java:4084)
  08-21 15:59:34.170: E/AndroidRuntime(1678):     at android.view.View$PerformClick.run(View.java:16966)
  08-21 15:59:34.170: E/AndroidRuntime(1678):     at android.os.Handler.handleCallback(Handler.java:615)
  08-21 15:59:34.170: E/AndroidRuntime(1678):     at android.os.Handler.dispatchMessage(Handler.java:92)
  08-21 15:59:34.170: E/AndroidRuntime(1678):     at android.os.Looper.loop(Looper.java:137)
  08-21 15:59:34.170: E/AndroidRuntime(1678):     at android.app.ActivityThread.main(ActivityThread.java:4745)
  08-21 15:59:34.170: E/AndroidRuntime(1678):     at java.lang.reflect.Method.invokeNative(Native Method)
  08-21 15:59:34.170: E/AndroidRuntime(1678):     at java.lang.reflect.Method.invoke(Method.java:511)
  08-21 15:59:34.170: E/AndroidRuntime(1678):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
  08-21 15:59:34.170: E/AndroidRuntime(1678):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
  08-21 15:59:34.170: E/AndroidRuntime(1678):     at dalvik.system.NativeStart.main(Native Method)
  </code></pre>
  
  <p>Hope there's a solution! Thank you so much!</p>