Removing ItemClickListener from table

Hi

Ihave an application which uses MVP design pattern. View contains the UI controls and presenter contains the control logic. In one of my have a table. There is bind method in the presenter which attaches listeners to the table whenever the view is loaded. Now in the bind method i attach the ItemClickListener to the table(I need to take the user to a different view when he clicks on the table columns). The user can also navigate back to the table view. The problem is when the user is navigating back to the table view the bind method will be called again and a new ItemclickListener will be attached. So when the item click event is fired the handling method gets called again and again(as many times as the user had done the navigation to the table view) . Is there a possible way to remove all the listeners of type “ItemClickListeners” on my table at Once. So that only one ItemClickListeners is active when ever the bind method gets called? Here is the code

public void bind() {

	logger.info("Executing DailyTasksPresenter.bind()");
                                        
                                      // Remove the application's custom listeners first
	display.getDailyTaskTable().removeTableListeners();

	display.getDailyTaskTable().addListener((VisitingDataListener) this);
	display.getDailyTaskTable().addListener(
			(TableColumnLayoutListener) this);
	display.getDailyTaskTable().addListener(
			new ItemClickEvent.ItemClickListener() {

				public void itemClick(ItemClickEvent event) {

					DailyTask task = (DailyTask) event.getItemId();

					if (event.getPropertyId().equals("expand")) {
						if (display.isDateInHeaderValid()) {
							if (task.getServiceTypeInfo().isExpandable()) {
								display.getDailyTaskTable()
										.expandThreatInfoAndServiceType(
												task);
							}
						}
					} else if (event.getPropertyId().equals("visit")) {
						if (display.isDateInHeaderValid()) {
							if (!task.isVisited()) {
								display.navigateToVisitingDataView(task);
							}
						}
					} else if (event.getPropertyId().equals("visitInfo")
							|| ((event.getPropertyId()
									.equals("serviceTypeInfo")))) {
						if (display.isDateInHeaderValid()) {
							display.navigateToClientView(task);
						}
					}
				}
			});
	// Get the cache object from view.
	dailyTasksCache = display.getDailyTasksCache();
}

You can do something like this :


for(Object listener : table.getListener(eventType)){
      table.removeListener(eventType, listener);
}

It will get the list of all listeners listening to events of type “eventType” (which is a Class<?>) and remove them one by one.

Thanks mathias for your response.
I am doing as you suggested as follows but I always get the size as 0. see what i am doing and suggest where i am getting it wrong. Even thougn the event is firing as many times as i explained earlier,

private void removeItemclickListeners() {
logger.info(“Item Listeners :” + display.getDailyTaskTable().getListeners(ItemClickListener.class).size());
for (Object listener : display.getDailyTaskTable().getListeners(ItemClickListener.class)) {
display.getDailyTaskTable().removeListener(ItemClickEvent.ItemClickListener.class, listener);
}

}

getListeners parameter is an event type not a listener type try with

getListeners(ItemClickEvent.class)

Thanks Mathias

That worked :slight_smile: !!!