title: I am having issues with displaying my custom objects in a ListView android
body: <p>The idea is to be able to store an object that is passed from the child activity into an array of objects and display it in a list view in the parent activity. I cannot think of how to implement this in the code I already have, but I know the logic of my code is wrong. </p>
  
  <p>This is my main activity.</p>
  
  <pre><code>public class MainActivity extends AppCompatActivity {
  
      //List&lt;Expense&gt; Expenses = new ArrayList&lt;Expense&gt;();
      ArrayList&lt;Expense&gt;  expenses;
      ListView listView;
  
      private static CustomArrayAdapter adapter;
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
          Toolbar myToolbar = (Toolbar) findViewById(R.id.toolbarLayout);
          setSupportActionBar(myToolbar);
  
          listView = (ListView)findViewById(R.id.listView);
  
          expenses= new ArrayList&lt;&gt;();
  
          adapter = new CustomArrayAdapter(expenses, getApplicationContext());
  
          listView.setAdapter(adapter);
      }
  
      @Override
      protected void onActivityResult(int requestCode, int resultCode, Intent data) {
          super.onActivityResult(requestCode, resultCode, data);
          if (requestCode == 1) {
              if(resultCode == Activity.RESULT_OK){
                  Expense expense = (Expense) data.getParcelableExtra("expense");
  
                  //THIS LINE HERE IS WHAT I AM ON ABOUT.
                  expenses.add(expense);
              }
              if (resultCode == Activity.RESULT_CANCELED) {
                  //Write your code if there's no result
              }
          }
      }
  }
  </code></pre>
  
  <p>So I created a separate class for the Custom Array Adapter. Here it is</p>
  
  <pre><code>public class CustomArrayAdapter extends ArrayAdapter&lt;Expense&gt; {
  
      private ArrayList&lt;Expense&gt; expenseDataSet;
  
      Context mContext;
  
      private static class ViewHolder {
          TextView txtAmount;
          TextView txtDateAdded;
          TextView txtDateOfExpense;
          TextView txtDes;
          TextView txtDatePaid;
          ImageView expenseImage;
      }
  
      public CustomArrayAdapter(ArrayList&lt;Expense&gt; data, Context context) {
          super(context, R.layout.list_item, data);
          this.expenseDataSet = data;
          this.mContext=context;
      }
      private int lastPosition = -1;
  
      @Override
      public View getView(int position, View view, ViewGroup parent) {
          //checking whether there is an instance of the view, inflate this view to make view not null
          Expense currentExpense = expenseDataSet.get(position);
  
          ViewHolder viewHolder; // view lookup cache stored in tag
  
          final View result;
  
          if (view == null)
          {
              viewHolder = new ViewHolder();
              LayoutInflater inflater = LayoutInflater.from(getContext());
              view = inflater.inflate(R.layout.list_item, parent, false);
              //setting the text views in list to the currentContact
  
              TextView amount = (TextView) view.findViewById(R.id.txtAmount2);
  
              TextView description= (TextView) view.findViewById(R.id.txtDescription);
  
              TextView dateOfExpense = (TextView) view.findViewById(R.id.txtDateOfExpense2);
  
              TextView dateAdded = (TextView) view.findViewById(R.id.txtDateAdded2);
  
              TextView datePaid = (TextView) view.findViewById(R.id.txtDatePaid2);
  
              ImageView ivExpenseImage = (ImageView) view.findViewById(R.id.ivExpenseImage);
  
              result=view;
  
              view.setTag(viewHolder);
          } else {
              viewHolder = (ViewHolder) view.getTag();
              result=view;
          }
  
          Animation animation = AnimationUtils.loadAnimation(mContext, (position &gt; lastPosition) ? R.anim.up_from_top : R.anim.down_from_top);
          result.startAnimation(animation);
          lastPosition = position;
  
          viewHolder.txtAmount.setText(currentExpense.get_amount());
          viewHolder.txtDes.setText(currentExpense.get_amountVat());
          viewHolder.txtDateOfExpense.setText(currentExpense.get_dateOfExpense());
          viewHolder.txtDateAdded.setText(currentExpense.get_dateAdded());
          viewHolder.txtDatePaid.setText(currentExpense.get_datePaid());
          viewHolder.expenseImage.setImageURI(currentExpense.get_imageUri());
  
          // Return the completed view to render on screen
          return view;
      }
  
  }
  </code></pre>