I have a GridView where each item shows information about an object. Next, I have an IntentService with some runnable tasks. Finally, there is a custom adapter that extends from BaseAdapter with an ArrayList for the items. The communication between the IntentService and the Gridview occurs across BroadcastReciver in the adapter that receives an special object called Thumb which is a Parcelable object that come as an extra of an Intent.
So far everything is typical.
Now, when a task starts in the service within its runnable object, I send an empty Thumb (as intent). So GridView shows to the user an item which represents the task that is executing at these moment, and when task finish the item will be refresh with the new information.
Here is my PROBLEM: How can I refresh dynamically the information of an object in a GridView?
Approach 1
My first idea (and I think the most expensive in performance) was: When an intent comes with an empty Thumb I will add it to ArrayList and I will notify to adapter. Then when second intent come with "full" Thumb, I will search it in the ArrayList and replaced it by the another.
- (+) Easy to implement.
- (-) Bad performance. I have to search an item in the ArrayList, checking that is the same object and then replace it.
Approach 2
If the "Empty Thumb" and the "Full Thumb" are the same (because in the Task only there is a Thumb object which is being updated), the reference (memory address) must be the same. So only need add the empty Thumb the first time to the GridView and then when comes the Full Thumb inside an intent, I will notify adapter for refresh it (because the ArrayList already has the Thumb (reference))
- (+) Great performance, because I don't need operate over ArrayList
- (-) Doesn't work. (yes, a BIG obstacle). So I have seen that if I compare references ( == ) between "Empty Thumb" and "Full Thumb" are different. I think this is because Thumb is a Parcelable object that executes in a runnable task in a different thread and when come to BroadcastReciver internally it creates a new object or clone the Thumb. So the objects are different. It is a "serializable" mechanism for pass objects between threads.
Any suggest about how should I focus this problem?