Solved

Android component listener

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

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

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Problem to Alipay 10 67
ejb message driven bean mdb creation steps 2 34
How to export SQL to firebase ? 7 107
how many cores in note3? 5 71
Often, people trade privacy and security for convenience. However in today's concrete jungle, this is an extremely foolish decision considering the vast amount of technologies being used against consumer interest. First off, I won't waste any time e…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
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 …

680 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