Bug 1738 - ListView.ItemClick and ListView.ItemLongClick are not assigned in the same way
Summary: ListView.ItemClick and ListView.ItemLongClick are not assigned in the same way
Status: RESOLVED FIXED
Alias: None
Product: Android
Classification: Xamarin
Component: BCL Class Libraries ()
Version: 1.0
Hardware: PC Windows
: --- normal
Target Milestone: ---
Assignee: Bugzilla
URL:
Depends on:
Blocks:
 
Reported: 2011-10-27 18:27 UTC by Sam Lippert
Modified: 2011-10-30 15:50 UTC (History)
4 users (show)

Tags:
Is this bug a regression?: ---
Last known good build:

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.


Please create a new report on Developer Community or GitHub with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:
Status:
RESOLVED FIXED

Description Sam Lippert 2011-10-27 18:27:20 UTC
In Mono for Android 1.9.1 and 1.9.2, it appears that AdapterView.ItemClick is assigned by providing an EventHandler<ItemEventArgs>, and AdapterView.ItemLongClick is assigned with a AdapterView.ItemLongClickHandler, which takes the same parameters as ItemEventArgs. I would expect ItemLongClick to also be an event, as that seems more C#-ified to me, and because that is the way that it is in 1.2.0.
Comment 1 Redth 2011-10-28 11:10:16 UTC
Ditto, this is a current show stopper for me.
Comment 2 Jonathan Pryor 2011-10-30 15:50:08 UTC
This is fixed in commit 3498b479. However, there are some changes, which may not matter in terms of backward compatibility.

First, background: the reason why AdapterView.ItemLongClick was transformed into a property in 1.9.0 was because it always should have been a property: the corresponding Java method (AdapterView.OnItemLongClickListener.onLongClick()) has a `boolean` return type:

	http://developer.android.com/reference/android/widget/AdapterView.OnItemLongClickListener.html#onItemLongClick(android.widget.AdapterView<?>, android.view.View, int, long)

The event handler delegate, meanwhile, had a `void` return type, and there was no mechanism to actually return a `boolean` value.

Result: The Java side was getting indeterminate garbage; it would be akin to this C fragment:

	/* note: `int` return type */
	typedef int (*HANDLER)(void);

	/* note: `void` return type */
	void my_func(void) {}

	void badfunc()
	{
		HANDLER h = (HANDLER) my_func;
		int n = h ();  /* what's the value of 'n'? */
	}

So this is Bad™, and changing AdapterView.ItemLongClick to a property fixed this, because the property had a `bool` return type, and everything could then be kosher.

So how do we make this an event again? By following the pattern of System.ComponentModel.HandledEventArgs -- we add a Handled property to the EventArgs class, and use the Handled property as the return value of AdapterView.OnItemLongClickListener.onItemLongClick():

	class AdapterView {
		public partial class ItemLongClickEventArgs : System.EventArgs {

			public ItemLongClickEventArgs (
					bool handled,
					Android.Widget.AdapterView parent,
					Android.Views.View view,
					int position,long id);

			// Note: has a setter!
			public bool Handled { get; set; }
			public Android.Widget.AdapterView Parent { get; }
			public Android.Views.View View { get; }
			public int Position { get; }
			public long Id { get; }
		}

		public event
			EventHandler<Android.Widget.AdapterView.ItemLongClickEventArgs>
			ItemLongClick;
	}

The default value of Handled will be `true`, so no change from client code should be needed from Mono for Android 1.2.0, but if you need to return `false` for some reason, you can now do so:

	adapter.ItemLongClick += (sender, e) {
		e.Handled = false;
	};

This change from properties to events has been made to the following members:

	Android.App.Dialog.KeyPress
	Android.App.FragmentBreadCrumbs.BreadCrumbClick
	Android.Media.MediaPlayer.Error
	Android.Media.MediaPlayer.Info
	Android.Preferences.Preference.PreferenceClick
	Android.Views.View.Drag
	Android.Views.View.GenericMotion
	Android.Views.View.Hover
	Android.Views.View.KeyPress
	Android.Views.View.LongClick
	Android.Views.View.Touch
	Android.Views.ViewTreeObserver.PreDraw
	Android.Widget.AdapterView.ItemLongClick
	Android.Widget.ExpandableListView.ChildClick
	Android.Widget.ExpandableListView.GroupClick
	Android.Widget.PopupMenu.MenuItemClick
	Android.Widget.PopupWindow.TouchIntercepted
	Android.Widget.ShareActionProvider.ShareTargetSelected
	Android.Widget.TextView.EditorAction
	Android.Widget.VideoView.Error