How to create change listener for variable?
Java gives you a simple Observer pattern implementation for this kind of thing, but you'll need to set your observed variable within a method that manages listener notifications. If you can't extend Observable, you can either use composition (i.e., have an Observable instance in your class to manage notifications), or you can take a look at java.util.Observable to get an idea of how to roll your own version.
Flux.java
import java.util.Observable;
public class Flux extends Observable {
private int someVariable = 0;
public void setSomeVariable(int someVariable) {
synchronized (this) {
this.someVariable = someVariable;
}
setChanged();
notifyObservers();
}
public synchronized int getSomeVariable() {
return someVariable;
}
}
Heraclitus.java
import java.util.Observable;
import java.util.Observer;
public class Heraclitus implements Observer {
public void observe(Observable o) {
o.addObserver(this);
}
@Override
public void update(Observable o, Object arg) {
int someVariable = ((Flux) o).getSomeVariable();
System.out.println("All is flux! Some variable is now " + someVariable);
}
}
This is one of the many reasons to hide variables behind setter/getter pairs. Then, in the setter you can notify your listener that this variable has been modified in the appropriate way. As the others have commented, there is no built in way to do exactly what you want, you need to implement it yourself.
Alternatively Benjamin brings up an interesting pattern, called the Decorator pattern, which might be useful to you if the code in question cannot be modified. You can look up more info at Wikipedia
The idea is to build a compatible wrapper around an object. Lets say your object in question is of type MyClass.
class MyClass{
public void doFunc(){...}
}
class MyLoggedClass extends MyClass{
MyClass myObject;
public void doFunc(){
//Log doFunc Call
myObject.doFunc();
}
}
instead of
MyClass object = new MyClass();
You would use
MyClass object = new MyLoggedClass(new MyClass());
Now your rest of the code would use object as per normal, except that each function call will be logged, or notified, etc.
As you will see in Wikipedia, this is typically done via an interface that the class in question inherits from, but this may not be possible in your case.
You could use View Model. First create a class that extends view model. I called mine NameViewModel.
import android.arch.lifecycle.MutableLiveData;
import android.arch.lifecycle.ViewModel;
public class NameViewModel extends ViewModel {
private MutableLiveData<String> currentName;
public MutableLiveData<String> getCurrentName(){
if(currentName == null){
currentName = new MutableLiveData<>();
}
return currentrName;
}
}
then in activity that the variable value change first define an instance of your NameViewModel class :
private NameViewModel mNameViewModel;
then in onCreate use this code snippet
mNameViewModel = ViewModelProvider.of(this).get(NameViewModel.class)
mNameViewModel.getCurrentName().observe(this, new Observer<String>() {
@Override
public void onChanged(@Nullable String name) {
//do what you want when the varriable change.
}
});
if you want to change the value of name you could use this code snippet:
mNameViewModel.getCurrentName().postValue(String newName);
I use postValue() since I want to update variable from worker thread if you are on UI thread you should use setValue(). Now every time the variable are changed it update the UI.