It is given: a certain ArrayList with several objects of the same type, and an Adapter inherited from the ArrayAdapter, which presents these objects in a ListView.
Required: implement the update of a specific object, and accordingly update the corresponding View in the ArrayAdapter.
I did it like this:
arrayList.get(...).setMyProperty(...); adapter.NotifyDataSetChanged(); True, at first for some reason it did not work - View was not updated.
I thought that this is due to the fact that the adapter somehow compares objects, and I don’t have the correct comparison mechanism in them , because it doesn’t detect changes and does not update the corresponding View, and I need to implement something like hashCode in these objects , equals or comparable , then it will work correctly.
But I found the solution easier - just add the following to my ArrayAdapter implementation, such as it is missing:
@Override public int getCount() { return values.size(); } As a result, the element really began to be updated. But after all, I still do not have a comparison mechanism, which means that when updating one element, getView () will be stupidly called for all elements.
I understand correctly that this will be so? (I'll check it later, just for now the element is only 1)
If I understand correctly, is this normal, or is it possible (necessary) to somehow implement a comparison so that getView is called only for views of updated objects?
Also, as I understand it, there is a ViewHolder pattern, which in this case allows you to get rid of inflate () and findViewById () with each update of each element, thereby, though not abandon getView for all elements, but optimize it.
But isn't it better not to call getView at all once again, but only for updated items?