Solved

Android component listener

Posted on 2013-05-11
2
469 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 28

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses

628 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