Reputation: 3149
Today I was told that we could introduce an MVC (or quasi-MVC) architecture to our Android project. I have some classes to contain info mostly inputted by users, and I want my Views (assuming it's TextView
, for the sake of discussion) to display it. The way I initially thought was, every time I updated my Data-containing class, I'd call a method to reflect it on my TextView
.
Data d = new Data();
TextView t = (TextView) findViewById(R.id.IamTextView);
// ....
d.setS("Foo"); // <--- Data updated!
t.setText(d.getS()); // <--- View updated, too!
This is undoubtedly lame, even though it would be fine as long as there were very limited cases when data was updated and we were aware of where they all were, but I want to try something cooler and smarter. So I'm defining an custom EventListener
...
public class Data {
protected int i;
protected double d;
protected String s;
//Setter & Getter omitted!
}
public interface Data.onUpdatedListener {
public void onUpdated (Data d);
}
public class TestActivitiy extends Activity implements Data.onUpdatedListener {
Date[] d;
//onCreate() omitted!
@Override public void onUpdated (Data d) {
// I want to reflect this change on my Views, like below.
TextView t = (TextView) findViewById(R.id.IamTextView);
t.setText(d.getS());
}
}
I know I have to make a dedicated class that serves as Controller, and the role is to notify Activity of an updated occurring and which object it is (perhaps it'd be super helpful if I make every member variables in Data
as parameters of onUpdated()
, so that I can only send "the difference" to the Activity, not an object as a whole).
My Issue 01:
I'm less than sure know how to notify my activity of update (in other words, how to fire onUpdated()
method).
My Issue 02: I'm less than sure how to determine an object is updated. If any of the members is different, I want to notify of it, but how to? Should we always preserve the latest state of an object, and compare all of their member variables with those of the current variables?
Upvotes: 1
Views: 1051
Reputation: 2486
In the class that you are going to update the Data
, define the listener and provide a setter method to the listener.
class Data {
A a;
B b;
C c;
//...constructor, setter, getter, etc
}
class A {
Listener listener;
interface Listener {
void onUpdate(Data data);
// another abstract method accepting A, B and C as parameters,
// just an example and can be omitted if onUpdate(Data) is sufficient
void onUpdate(A a, B b, C c);
}
public void setListener(Listener listener) {
this.listener = listener;
}
public void update(Data data) { // the method that is to update the Data
if (listener != null) {
listener.onUpdate(data);
listener.onUpdate(data.a, data.b, data.c);
}
}
public void update(A a, B b, C c) { // another method to update the Data
if (listener != null) {
// Assume you have this constructor for Data,
// just for the ease of understanding.
listener.onUpdate(new Data(a, b, c));
listener.onUpdate(a, b, c);
}
}
}
class B implements A.listener {
// In somewhere
setListner(this);
@Override
void onUpdate(Data data) {
// your implementation
}
@Override
void onUpdate(A a, B b, C c) {
// your implementation
}
}
EDITED Added another callback method in the listener for demonstration of the usage of the listener.
Upvotes: 3