Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom event listener on Android app

I need to set up a simple event listener to refresh a ListView once in a while. The problem is I don't know how could I generate an event.

I know that for events like key or button pressing I just need to implement the Handler. But in this specific case I actually need to generate the event, which will be fired every time another running thread of my app wakes up and refreshes its list of news from an RSS feed.

I've done everything, but got stuck in here. Can I get any suggestion or link with some more info on how to implement this?

like image 729
Bilthon Avatar asked Jun 06 '10 05:06

Bilthon


People also ask

How do you call a listener on Android?

Yes, you may call the listener, which in the pattern above is YourActivity . Exactly, YourActivity = listener. So, you may even call this listener's onClick(View) callback method.

What are event listeners in Android?

An event listener is an interface in the View class that contains a single callback method. These methods will be called by the Android framework when the View to which the listener has been registered is triggered by user interaction with the item in the UI.

How do you implement listener in Kotlin?

When we're dealing with listeners, let's say the OnclickListener for views, thanks to optimizations that Kotlin do over Java libraries, we can turn this: view. setOnClickListener(object : View. OnClickListener { override fun onClick(v: View?) { toast("View clicked!") } })


2 Answers

  1. Define a callback interface

            public interface NewsUpdateListener 
            {
                void onNewsUpdate(<News data to be passed>);
            }
    
  2. 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);
            }
        ....
        }
    
  3. Fire the callback when news is available

    ....
    for (listener : listeners) 
    {
        listener.onNewsUpdate(<News data to be passed>);
    }
    ....
    
  4. Register listener somewhere during initialization

    ....
        <class <Background processing class object>.registerListener
    (
        new OnNewsUpdateListener() {
            onNewsUpdate(<News Data>) {
                // process news data
                runOnUIThread(new Runnable() {
                    public void run() {
                        // refresh list view
                    }
                }
            }
    }
    ....
    
like image 104
Sameer Avatar answered Oct 20 '22 08:10

Sameer


try this:

interface MyHandlerInterface
{
   void onHandle(Object obj)
}
class myListClass
{
   MyHandlerInterface myHandler;
   public void setHandlerListener(MyHandlerInterface listener)
   {
      myHandler=listener;
   }
   protected void myEventFired(myObj)
   {
      if(myHandler!=null)
         myHandler.onHandle(myObj);
   }
}
like image 28
Hojjat Avatar answered Oct 20 '22 07:10

Hojjat