The array list we pass to a custom array adapter are we supposed to modify it in a thread safe way?
I mean if I have private class MyAdapter extends ArrayAdapter<CustomObject>
and I instantiate it with my ArrayList
of CustomObject
s if I later want to modify that array list or objects in the array list in order to then notify the adapter that the UI should be updated should I do it in a thread safe way? E.g. passing a synchronized array list?
相关问题
- Delete Messages from a Topic in Apache Kafka
- Jackson Deserialization not calling deserialize on
- How can I create this custom Bottom Navigation on
- How to maintain order of key-value in DataFrame sa
- StackExchange API - Deserialize Date in JSON Respo
If you modify the list on the main/UI thread, then go ahead. The ListView itself operates also on the UI thread.
If you change the list from another thread, you may have to handle synchronization issues. Though it should not cause any problems when the ListView is not scrolling, i.e. does not access the Adapter.
To change the list anytime from another thread, you have to post all changes to the UI thread.
If it's too complicated to determine what elements need to change, you also could create a new list and a new adapter:
No, the UI is updated in the UI thread when you call notifyDataSetChanged(). Its handled by android properly
Since it is a custom adapter,what if you were to maintain 2 array lists.
and you do all your changes to the modifiableList,
Then when you can determine the user is not scrolling your UI, you can call
notifyDatasetChangedCustom()
EDIT: if you are doing it from another thread,you could maintain a WeakReference to your adapter and then call the changes.
You could also try looking into AsyncTaskLoader.This would take care addition of items and view updating automatically,although the implementation would be more complicated.