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
Solved

Android component listener

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
ejb wildfly example 2 22
How to Implement Two Event Listeners in Android 2 45
How to determine if a string is a valid SHA value 7 35
jsp error 6 25
In this article we will discuss all things related to StageFright bug, the most vulnerable bug of android devices.
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

856 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