Refactored Donate

The Donate activity can now be completely refactored to make use of the Base class.

  1. public class Donate extends Base
  2. {
  3. private RadioGroup paymentMethod;
  4. private ProgressBar progressBar;
  5. private NumberPicker amountPicker;
  6. private TextView amountText;
  7. private TextView amountTotal;
  8. @Override
  9. protected void onCreate(Bundle savedInstanceState)
  10. {
  11. super.onCreate(savedInstanceState);
  12. setContentView(R.layout.activity_donate);
  13. paymentMethod = (RadioGroup) findViewById(R.id.paymentMethod);
  14. progressBar = (ProgressBar) findViewById(R.id.progressBar);
  15. amountPicker = (NumberPicker) findViewById(R.id.amountPicker);
  16. amountText = (TextView) findViewById(R.id.amountText);
  17. amountTotal = (TextView) findViewById(R.id.amountTotal);
  18. amountPicker.setMinValue(0);
  19. amountPicker.setMaxValue(1000);
  20. progressBar.setMax(target);
  21. }
  22. public void donateButtonPressed (View view)
  23. {
  24. String method = paymentMethod.getCheckedRadioButtonId() == R.id.PayPal ? "PayPal" : "Direct";
  25. int donatedAmount = amountPicker.getValue();
  26. if (donatedAmount == 0)
  27. {
  28. String text = amountText.getText().toString();
  29. if (!text.equals(""))
  30. donatedAmount = Integer.parseInt(text);
  31. }
  32. if (donatedAmount > 0)
  33. {
  34. newDonation(new Donation(donatedAmount, method));
  35. progressBar.setProgress(totalDonated);
  36. String totalDonatedStr = "$" + totalDonated;
  37. amountTotal.setText(totalDonatedStr);
  38. }
  39. }
  40. }

Replace your version with this and execute it - fix any missing import statements necessary.

Look carefully at the changes to this version over the previous attempt.