Refactored Report - New ‘Class’

Finally, rework the Report class to remove the hard coded values - and use a different ‘adapter’

  1. public class Report extends Base {
  2. private ListView listView;
  3. @Override
  4. public void onCreate(Bundle savedInstanceState) {
  5. super.onCreate(savedInstanceState);
  6. setContentView(R.layout.activity_report);
  7. listView = (ListView) findViewById(R.id.reportList);
  8. DonationAdapter adapter = new DonationAdapter(this, donations);
  9. listView.setAdapter(adapter);
  10. }
  11. }

This is the new adapter - DonationAdapter. You can place this at the end of the Report class (outside the closing brace) if you like:

  1. class DonationAdapter extends ArrayAdapter<Donation>
  2. {
  3. private Context context;
  4. public List<Donation> donations;
  5. public DonationAdapter(Context context, List<Donation> donations)
  6. {
  7. super(context, R.layout.row_donate, donations);
  8. this.context = context;
  9. this.donations = donations;
  10. }
  11. @Override
  12. public View getView(int position, View convertView, ViewGroup parent)
  13. {
  14. LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  15. View view = inflater.inflate(R.layout.row_donate, parent, false);
  16. Donation donation = donations.get(position);
  17. TextView amountView = (TextView) view.findViewById(R.id.row_amount);
  18. TextView methodView = (TextView) view.findViewById(R.id.row_method);
  19. amountView.setText("" + donation.amount);
  20. methodView.setText(donation.method);
  21. return view;
  22. }
  23. @Override
  24. public int getCount()
  25. {
  26. return donations.size();
  27. }
  28. }

If all goes well - then you should be able to make donations, and then see a list of them in the report activity.