I am having troubles using java.util.Observable
in my android application.
In my app, I want to update a listview
when the data is changed in the background.
So the objects from the listview
are in the observer. I see them in the arraylist
from the observer class. In Eclipse Debugging I see the reference to my objects, for example: at.stockserv.todo.Todo@1a3f2cb7
One thing I don't understand: When the data is changed 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 reference. So in my listview
, still the old data is stored with the old reference.
My code in the 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 the arraylist
with data in the adapter
. The data is here not changed, but the data should be changed from the first method.
My code of the observer is:
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));
}
}
What I don't understand is:
The object in the arraylist
is added to the observer. Some data in the observer object is changed. But the changes are only in the object of the observer, not in the arraylist which I added to the observer. I thought that in the observer there is just a reference to my object in the arraylist
? So the object in the arraylist
should change as well when the object in the observer is changed. But this does not work, why?
Can anyone explain me what is happening here?
Once your data changes call the bellow method.
CustomAdapter adapter = new CustomAdapter(YourActivity.this, dataSet); adapter.notifyDataSetChanged();
Here your notifying the list adapter that dataSet has been changed.
I now make the whole update process with LocalBroadcasts, now my problem is solved.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With