1

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?

4

1 回答 1

0

方法 3

最后我使用了一个中间解决方案(经常发生)。

“空拇指”在 GridView 中具有所有相同的视图。

因此,当它向 ArraList 添加一个 Empty Thumb 时,它还会添加其他列表,以保持 Empty Thumb 在 ArrayList 中的位置。

接下来,当“完整拇指”到达时,我得到这个新位置列表的第一个位置,然后在 ArrayList 中用该位置的完整拇指替换空拇指,最后我从位置列表中删除这个位置。

于 2013-09-15T08:49:05.257 回答