0

I need a little help with my Interface. I think that i doesn't understand them at all. So i created this interface to notify every classes that implements it when a certain event occurs.

public interface OnColorsChangeListener {
    void onColorsChangeListener(ColorsProp colorsProp);
}

My class that hold the interface:

   private OnColorsChangeListener mCallback;

... // other code

    // the event occurs here so i call:
    mCallback.onColorsChangeListener(mProps);
    // but of course here i get an NPE becouse this is undefined in this class.. well,     with some replies here i'll understand better how to use that for reach my point

The class that implements it:

public class ClassTest implements OnColorsChangeListener {

... // other code

@Override
public void onColorsChangeListener(ColorsProp colorsProp) {
    Log.d(TAG, "Color changed! " + colorsProp.color);
}

i put this in 4/5 classes to be notified in the same time for the color change. I'm quite sure the reason is that I didn't understand very well how them works, so can anyone point me to the right direction? Thank you!

iGio90
  • 3,251
  • 7
  • 30
  • 43
  • What is the class that holds the interface supposed to achieve? You might want to read the [Java tutorial on interfaces](http://docs.oracle.com/javase/tutorial/java/concepts/interface.html). – keyser Jun 21 '14 at 10:42
  • the class that should notify the others is the one with private OnColorsChangeListener mCallback; – iGio90 Jun 21 '14 at 10:45
  • It sounds to me like you want to use a BroadcastReceiver and then have those registered in all places where you want to receive this event simultaneously. http://developer.android.com/reference/android/content/BroadcastReceiver.html – zoltish Jun 21 '14 at 10:52

2 Answers2

1

Explanation by example:

You have to instantiate your callback, & it has to be an instance of your class

private OnColorsChangeListener mCallback;

mCallback = new ClassTest();


mCallback.onColorsChangeListener(mProps);

However if you want multiple callbacks you will need to use the Observer pattern. Simple example:

private List<OnColorsChangeListener> mCallbacks = new ArrayList<OnColorsChangeListener>();

mCallbacks.add(new ClassTest());
mCallbacks.add(new OtherClass());

for(OnColorsChangeListener listener : mCallbacks) {
   listener.onColorsChangeListener(mProps);
}

Obviously if you have the class, somewhere else you would not new it up, you would use that reference:

mCallbacks.add(mClassTest);

Observer Pattern Wikipedia

Blundell
  • 75,855
  • 30
  • 208
  • 233
  • Thanks for the explaination! – iGio90 Jun 21 '14 at 10:55
  • side note: callbacks and listeners mean slightly different things (also observers). Try not to mix them up like the above. Callbacks are for when 1 thing has finished & you get a callback at the end. Listeners more often can be called after or between an event & multiple times etc – Blundell Jun 21 '14 at 10:58
  • great, so i definitely need a listener :D – iGio90 Jun 21 '14 at 10:58
0

An interface is just a way to group together a bunch of related methods. Implementing this interface then requires you to implement all the methods grouped together by the interface.

The Java Tutorials has a good read on the subject:

What is an interface?

Here's a Stackoverflow thread regarding listener interfaces in android:

How to create our own Listener interface in android?

In short, you don't use the interface directly since it only specifies which methods implementing classes are supposed to implement.

Community
  • 1
  • 1
keyser
  • 18,829
  • 16
  • 59
  • 101
  • well ok... i think I undestand now! so I'm facing the wrong way to achieve my score. Maybe you can point me to a method to notify some classes when an event occurs? (Without using broadcasts) – iGio90 Jun 21 '14 at 10:53
  • @iGio90 Blundell gave a good example of how to do this in your case, as I'm sure you noticed :p – keyser Jun 21 '14 at 11:24