Question

I am currently doing an android application (new to Android) and I saw that instead of creating new objects I am able to pass my string data directly to my adapter

    listItem = new ArrayList<listItems>();
    ///this addField is another class in which my data base stored an arraylist of results
     for (addField field : groupList) {

         item = new listItems(field.getFieldOne(),field.getFieldTwo(),field.getFieldThree(),field.getFieldFour(),field.getFieldFive(),0);

              item.setFieldOne(field.getFieldOne());
              item.setFieldTwo(field.getFieldTwo());
              item.setFieldThree(field.getFieldThree());
              item.setFieldFour(field.getFieldFour());
              item.setFieldFive(field.getFieldFive());

                  listItem.add(item);

                 //adapter.add(item);  <-- code in question


            adapter.notifyDataSetChanged();

what is the difference between passing my objects to the array list and then notifying the adapter to change the data or directly passing the object to the adapter?

I am guessing since the adapters data source is connected to the listItems class it still will see the data change but is there an advantage to directly passing it to the list view adapter?

Was it helpful?

Solution

It's pretty much the same thing. Check the source and see for yourself: https://github.com/android/platform_frameworks_base/blob/master/core/java/android/widget/ArrayAdapter.java

If you call this on the adapter:

setNotifyOnChange(true)

...then notifyDataSetChanged() is automatically called for you as well when using the adapter's add() method.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top