문제

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?

도움이 되었습니까?

해결책

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top