I'm having problems using java.util.Observable
in an android app. In my application, I want to update the listview
when the data changes in the background.
So, the objects from listview
are in the observer. I see them in the arraylist
from the observer class. In Eclipse Debugging, I see a link to my objects, for example: at.stockserv.todo.Todo@1a3f2cb7
One thing I donβt understand: when data changes in my observer update method
@Override public void update(Observable observable, Object data) { if (observable instanceof DBObjectUpdateObserver) { ObserverMessage message = (ObserverMessage) data; if (message.getAction() == Actions.messageIdChanged) { if (message.getData().get("oldId").equals(Integer.valueOf(getId()))) { setId((Integer) message.getData().get("newId")); DBObjectUpdateObserver.getInstance().dataHasChanged(this); } } } }
After setId, suddenly the object has another link. Therefore, in my listview
, still the old data is stored with the old link.
My code in view:
@Override public void update(Observable observable, Object data) { ObserverMessage message = (ObserverMessage) data; if (message.getAction() == Actions.messageDBDataChanged) { if (actNotices.contains(message.getData().get("dbObject"))) { notifyDataSetChanged(); Toast.makeText(getApplicationContext(), "Data is updated ", Toast.LENGTH_LONG).show(); } } }
actNotices
ist arraylist
with data in adapter
. The data here is not changed, but the data must be changed from the first method.
My observer code:
public class DBObjectUpdateObserver extends Observable { private static DBObjectUpdateObserver instance = new DBObjectUpdateObserver(); public static DBObjectUpdateObserver getInstance() { return instance; } private DBObjectUpdateObserver() { } public void updateId (DBCommunicator dbObject, int oldId, int newId) { HashMap<String, Object> data = new HashMap<String, Object>(); data.put("oldId", oldId); data.put("newId", newId); data.put("dbObject", dbObject); getInstance().setChanged(); getInstance().notifyObservers(new ObserverMessage(Actions.messageIdChanged, data)); } public void dataHasChanged(DBCommunicator dbObject, int oldId, int newId) { HashMap<String, Object> data = new HashMap<String, Object>(); data.put("dbObject", dbObject); data.put("oldId", oldId); data.put("newId", newId); getInstance().setChanged(); getInstance().notifyObservers(new ObserverMessage(Actions.messageDBDataChanged, data)); }
}
I do not understand: The object in the arraylist
added to the observer. Some data in the observer object has been changed. But changes occur only in the object of the observer, and not in the archarist, which I added to the observer. I thought that in the observer there is only a reference to my object in arraylist
? Thus, the object in the arraylist
must also change when the object in the observer is changed. But that doesn't work, why?
Can someone explain to me what is going on here?