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?
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.
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.
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!") } })
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);
}
....
}
Fire the callback when news is available
....
for (listener : listeners)
{
listener.onNewsUpdate(<News data to be passed>);
}
....
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
}
}
}
}
....
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);
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With