Solved

Android component listener

Posted on 2013-05-11
2
435 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
This is an explanation of a simple data model to help parse a JSON feed
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

863 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

24 Experts available now in Live!

Get 1:1 Help Now