Question

I think the question says it all: I want to create a custom OnItemClickListener for a ListView. I want to add and change the parameters for a OnItemClickListener but how can I create an own so it is called if I click on an Item in the ListView?

Was it helpful?

Solution

You create an interface first, then implement a method, where you set the listener in your custom class and already then make a main class to listen for your custom event

Interface:

public interface OnCustomEventListener{
  public void onEvent();   //can have parameters
}

method in your e.g. adapter: private OnCustomEventListener mListener; //field

//setter method
public void setCustomEventListener(OnCustomEventListener eventListener) {
   this.mListener=eventListener;
}

listener:

someObjectYouWantToHaveYourCustomListenerToBeAssignedFor.setCustomEventListener(new OnCustomEventListener(){
    public void onEvent(){
    //do whatever you want to do when the event is performed.
    }
});  

how to call e.g. from your adapter:

if(this.mListener!=null){
   this.mListener.onEvent();
}

P.S. Your custom listener may have as many parameters as you want

Source

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top