Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>There is no way in Java to be notified when a variable or class field changes. What you need to do is to implement a simple wrapper class for your int so that clients can register for callbacks whenever the value changes. This class might look something like this:</p> <pre><code>package com.example.android; /** * A store of an int value. You can register a listener that will be notified * when the value changes. */ public class IntValueStore { /** * The current value. */ int mValue; /** * The listener (you might want turn this into an array to support many * listeners) */ private IntValueStoreListener mListener; /** * Construct a the int store. * * @param initialValue The initial value. */ public IntValueStore(int initialValue) { mValue = initialValue; } /** * Sets a listener on the store. The listener will be modified when the * value changes. * * @param listener The {@link IntValueStoreListener}. */ public void setListener(IntValueStoreListener listener) { mListener = listener; } /** * Set a new int value. * * @param newValue The new value. */ public void setValue(int newValue) { mValue = newValue; if (mListener != null) { mListener.onValueChanged(mValue); } } /** * Get the current value. * * @return The current int value. */ public int getValue() { return mValue; } /** * Callbacks by {@link IntValueModel}. */ public static interface IntValueStoreListener { /** * Called when the value of the int changes. * * @param newValue The new value. */ void onValueChanged(int newValue); } } </code></pre> <p>Now you need to have some class that implements the IntValueStoreListener interface. You could let that be the <code>Activity</code>, which would then keep track of a <code>TextView</code> to update. I would implement a trivial custom <code>TextView</code> instead, like this:</p> <pre><code>package com.example.android; import com.example.android.IntValueStore.IntValueStoreListener; import android.content.Context; import android.util.AttributeSet; import android.widget.TextView; public class IntTextView extends TextView implements IntValueStoreListener{ public IntTextView(Context context) { super(context); } public IntTextView(Context context, AttributeSet attrs) { super(context, attrs); } public void onValueChanged(int newValue) { // The int got a new value! Update the text setText(String.valueOf(newValue)); } } </code></pre> <p>You can now setup your layout XML. For example:</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" &gt; &lt;Button android:id="@+id/btn_increment" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Increment" /&gt; &lt;Button android:id="@+id/btn_double" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Double" /&gt; &lt;com.example.android.IntTextView android:id="@+id/text_current_value" android:layout_width="match_parent" android:layout_height="wrap_content" /&gt; &lt;/LinearLayout&gt; </code></pre> <p>An activity that does the necessary setup would look like this:</p> <pre><code>package com.example.android; import android.app.Activity; import android.os.Bundle; import android.view.View; public class IntValueStoreActivity extends Activity { private IntValueStore mIntValueModel = new IntValueStore(1); @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); // Initialize the text view with the initial value IntTextView intTextView = (IntTextView)findViewById(R.id.text_current_value); intTextView.setText(String.valueOf(mIntValueModel.getValue())); // Setup the text view as a listener so it gets updated whenever the int // value changes mIntValueModel.setListener(intTextView); // Setup an OnClickListener that will increment the int value by 1. The // TextView will be automatically updated since it is setup as a // listener to the int value store findViewById(R.id.btn_increment).setOnClickListener(new View.OnClickListener() { public void onClick(View view) { mIntValueModel.setValue(mIntValueModel.getValue() + 1); } }); // Setup an OnClickListener that will double the int value. The TextView // will be automatically updated since it is setup as a listener to the // int value store findViewById(R.id.btn_double).setOnClickListener(new View.OnClickListener() { public void onClick(View view) { mIntValueModel.setValue(mIntValueModel.getValue() * 2); } }); } } </code></pre> <p>This will give you the following UI:</p> <p><img src="https://i.stack.imgur.com/n4fWH.png" alt="App screenshot"></p> <p>Whenever you click any of the buttons, the <code>TextView</code> will almost magically update the value it displays, even though none of the OnClickListeners have any code to touch the <code>TextView</code>!</p> <p>This is actually a common pattern in programming called <a href="http://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller" rel="noreferrer">Model-view-controller</a>. In the sample code above, the Model is <code>IntValueStore</code> and the View is <code>IntTextView</code> and there is no Controller.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload