Creating a custom event listener on an Android app

Define a callback interface public interface NewsUpdateListener { void onNewsUpdate(<News data to be passed>); } Provide a registration facility on the background thread which gets the RSS feed class <Background processing class name> { …. ArrayList<NewsUpdateListener> listeners = new ArrayList<NewsUpdateListener> (); …. public void setOnNewsUpdateListener (NewsUpdateListener listener) { // Store the listener object this.listeners.add(listener); } … Read more

addEventListener gone after appending innerHTML

Any time you set the innerHTML property you are overwriting any previous HTML that was set there. This includes concatenation assignment, because element.innerHTML += ‘<b>Hello</b>’; is the same as writing element.innerHTML = element.innerHTML + ‘<b>Hello</b>’; This means all handlers not attached via HTML attributes will be “detached”, since the elements they were attached to no … Read more

Listen to multiple keydowns

Fiddle: http://jsfiddle.net/ATUEx/ Create a temporary cache to remember your key strokes. An implementation of handling two keys would follow this pattern: <keydown> Clear previous time-out. Check whether the a key code has been cached or not.If yes, and valid combination: – Delete all cached key codes – Execute function for this combination else – Delete … Read more

reactjs event listener beforeunload added but not removed

The removeEventListener should get the reference to the same callback that was assigned in addEventListener. Recreating the function won’t do. The solution is to create the callback elsewhere (onUnload in this example), and pass it as reference to both addEventListener and removeEventListener: import React, { PropTypes, Component } from ‘react’; class MyComponent extends Component { … Read more

To pass a parameter to event listener in AS3 the simple way… does it exist?

Out of the box: it only takes 2 extra lines of elegant code to solve this ancient puzzle. stage.addEventListener(MouseEvent.CLICK, onClick(true, 123, 4.56, “string”)); function onClick(b:Boolean, i:int, n:Number, s:String):Function { return function(e:MouseEvent):void { trace(“Received ” + b + “, ” + i + “, ” + n + ” and ” + s + “.”); }; … Read more

Javascript multiple dynamic addEventListener created in for loop – passing parameters not working

Problem is closures, since JS doesn’t have block scope (only function scope) i is not what you think because the event function creates another scope so by the time you use i it’s already the latest value from the for loop. You need to keep the value of i. Using an IIFE: for (var i=0; … Read more