views:

55

answers:

1

Hi,

I've read up quite a bit on the exception thrown while using BaseAdapter, but i can't seem to find an alternative solution: Make sure the content of your adapter is not modified from a background thread

What i want to achieve is to keep a copy of a message queue in memory, and use this message queue to populate the BaseAdapter for my ListView. The reason im doing this is that the message queue will keep getting messages from a socket even when the ListView is not currently present (for example a chat window).

The problem comes when i have the Activity with the ListView in foreground, BaseAdapter binded to the message queue's data, and a message comes in the socket. Adding the new message into the queue will throw the exception mentioned above. Unless i pre-populate my BaseAdapter with the message queue (as in the BaseAdapter having its own message queue) and updating both of them when a new message come in, i can't really find a way around this issue.

I don't really want to double up the effort on keeping those 2 queues up-to-date like this, surely there is a better way of doing this? Send broadcasts around doesn't work either because of the potential delay in the adapter serving a scroll and the notifyDataSetChanged call is made.

A: 

Use a Handler to modify the "message queue" from the main application thread.

CommonsWare
i was thinking of doing it this way, but that means any messages coming from the network layer will have to be broadcasted for the Activity to pick up. If the Activity is not in the foreground (or in the activity stack), the message will not be processed. Does that sound right?
Bundee
@Bundee: "but that means any messages coming from the network layer will have to be broadcasted for the Activity to pick up." -- in principle, you can use a `Handler` in a `Service` for this, though I have not tried it.
CommonsWare