I am developing a sort of monitor which should update a lot of imageviews (fake leds) basing on what i receive from a machine and let the user know the current machine state. Is there a way to set on the variable a value listener which trigger a method when the value change in which i can update my UI?
-
better to write your code sample which you have done so for and let us know where you are facing issue. – Junaid Hafeez Jan 13 '17 at 11:33
-
1one option is data binding – Pavneet_Singh Jan 13 '17 at 11:34
-
1something like [http://stackoverflow.com/questions/14457711/android-listening-for-variable-changes](http://stackoverflow.com/questions/14457711/android-listening-for-variable-changes)? – Stefan Lindner Jan 13 '17 at 11:46
-
You need to implement an `Observer` pattern as described [here](http://stackoverflow.com/questions/39948709/android-custom-listener-for-an-event/39948968#39948968) – Onik Jan 14 '17 at 01:48
1 Answers
Thanks everybody for the help and the comments;
I find out Data Binding was the best option in my case, so after a few attempts i've been able to build a sample project where i've got a an handler which every 1sec increment an integer value by 1. After that i check if the value is pair or dispair and i set a boolen true or false.
I set up a layout file in which i've got 2 text view and a toggle button. The first text view text is binded to a simple string with my name, the second one is binded to the integer which is incremented each second (a timer) and the Toggle Button is binded to the Boolean.
Here's the Code:
MainActivity
public class MainActivity extends AppCompatActivity {
ActivityMainBinding binding;
android.os.Handler Handler;
User user;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding = DataBindingUtil.setContentView(this, R.layout.activity_main);
user = new User("Lorenzo", "Gangemi", 0);
binding.setUser(user);
useHandler();
}
public void useHandler() {
if (Handler != null) {
Handler.postDelayed(RunableHandler, 1000);
Handler.removeCallbacks(RunableHandler);
}
try {
Handler = new Handler();
} catch (Exception e) {
Handler = new Handler(Looper.getMainLooper());
}
Handler.postDelayed(RunableHandler, 1000);
}
private Runnable RunableHandler = new Runnable() {
@Override
public void run() {
user.increment_i();
binding.setUser(user);
useHandler();
}
};
}
User
public class User {
private final String firstName;
private final String lastName;
private boolean pari;
private int i;
public User(String firstName, String lastName, int i) {
this.firstName = firstName;
this.lastName = lastName;
this.i = i;
setPari(checkPari());
}
public String getFirstName() {
return firstName;
}
public String getLastName() {
return lastName;
}
public int getI() {
return i;
}
public void increment_i()
{
i++;
setPari(checkPari());
}
public boolean checkPari()
{
return i%2==0;
}
public boolean isPari() {
return pari;
}
public void setPari(boolean pari) {
this.pari = pari;
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">
<data>
<variable name="user" type="mdptech.databinding.User"/>
</data>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@{user.firstName}"/>
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@{Integer.toString(user.i)}" />
<ToggleButton
android:text="ToggleButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/toggleButton"
android:drawableBottom="@drawable/toggle"
android:background="@android:color/transparent"
android:textOff="OFF"
android:textOn="ON"
android:checked="@{user.pari ? true : false}"/>
</LinearLayout>
</layout>
Everything works fine.
As you can see from the code, in the handler Runnable i call the method for increment the timer, but i have to call binding.setUser(user);
too for updating the layout.
Is there a way to make this update happen automatically and not becouse i call a method?
That's not a big problem becouse i can create a separate handler which loops every 100ms (for ex.) and in his runnable call binding.setUser(user);
, But it will speed up my work a lot.
Thank you.

- 3,110
- 1
- 22
- 47