Solved

Android component listener

Posted on 2013-05-11
2
432 Views
Last Modified: 2013-05-15
I have a 'trick' that I do sometimes in Java/Swing in which I create a swing component that is always invisible and it may have any number of event listeners attached to it. When a variable or variables changes value, the code will cause that component to change and anything listening will know that those variable(s) have change.

I want to do this same thing in Android. What would I use instead of the swing component?
0
Comment
Question by:allelopath
2 Comments
 
LVL 26

Accepted Solution

by:
dpearson earned 500 total points
ID: 39160341
I think for Android this would be a case of building your own.  It doesn't sound very complicated - I think it would be something like:

public class DataStore {
     private List<UpdateListener> m_Listeners = new ArrayList<UpdateListener>() ;

     private Object m_Value ;

     public Object getValue() { return m_Value ; }
     public void setValue(Object newValue) { m_Value = newValue ; notifyListeners() ; }

     public void addListener(UpdateListener listener) { m_Listeners.add(listener) ; }
     public void removeListener(UpdateListener listener) { m_Listeners.remove(listener) ; }

     private void notifyListeners() {
          for (UpdateListener listener : m_Listeners) {
              try {
                  listener.dataValueChanged(this) ;
             } catch (Exception ex) { // Should log this }
          }
     }
}

Then your listeners would implement the interface:

interface UpdateListener {
    public void dataValueChanged(DataStore store) ;
}

and be notified any time the value was changed.

If you want to do this but make it typesafe (instead of just storing and getting an Object), you could use generics for the type instead of "Object" but it's the same pattern either way.

Doug
0
 
LVL 1

Author Comment

by:allelopath
ID: 39169317
Thanks, you are correct, one has to 'roll his own'
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Once again I push the limits of my phone.  An introduction to the Android Google Now Launcher.
This is an explanation of a simple data model to help parse a JSON feed
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now