when I call addAdapter() several times in background, sometimes I got some duplicate message. e.g. when I call addAdapter(item1, item2, item3…), it prints item1, item2, item2…
ExaminationItem currentAddItem = null;
private void addAdapter(ExaminationItem item)
{
currentAddItem = item;
addhandler.sendEmptyMessage(1);
}
private Handler addhandler = new Handler() {
@Override
public void handleMessage(Message msg)
{
switch (msg.what) {
case 1:
if (currentAddItem != null) {
_adapter.add(currentAddItem);
Log.i(getClass().getName(), "---------------------------addhandler: currentAddItem._itemName = " + currentAddItem._itemName);
}
break;
default:
break;
}
}
};
That isn’t surprising. Every time you call
sendEmptyMessage(), you’re adding a message to the thread’s message queue. You’re not adding your item to the queue, you’re just sending a message to the Handler to access whatever the value ofcurrentAddItemis at the time that the Handler processes the message. It doesn’t get to see what the value was at the time that you sent the message. So you’re likely to see both skipped items and duplicate items.