Note that there are some explanatory texts on larger screens.

plurals
  1. POPassing custom object between Android activities in C#
    text
    copied!<p>I am creating an android app in VS2012 using Xamarin.Android. I am displaying a custom list in Main screen. I need to pass a custom object(with ID,String,String,String properties) from this Main activity to another when user clicks on list item.</p> <p>Can anyone please help me with some example?</p> <p>edit:</p> <p>I have already tried solution mention in <a href="https://stackoverflow.com/questions/8763810/monodroid-sharing-non-trivial-data-between-activities">other question</a></p> <p>but the problem is I am getting below exception:</p> <p>This is how I am <strong>extracting in second activity</strong></p> <pre><code>InsuranceReminderBO i = (InsuranceReminderBO)Intent.GetSerializableExtra("SelectedItemID"); </code></pre> <p><strong>i is null</strong></p> <p>and in <strong>first activity setting</strong> it like this:</p> <pre><code>Intent intent = new Intent(this, typeof(ReminderDetails)); intent.PutExtra("SelectedItemID", selectedInsurance); StartActivity(typeof(ReminderDetails)); </code></pre> <p>where <code>class InsuranceReminderBO</code> is defined as</p> <pre><code>public class InsuranceReminderBO : Java.Lang.Object, Java.IO.ISerializable </code></pre> <p>I have also tried using <code>IParcelable</code> but in that I got error <code>Creator is not defined in ICreator or Creator</code></p> <p>Following the implementation of Iparcelable on CustomObject</p> <pre><code>'public class InsuranceReminderBO : Java.Lang.Object, IParcelable { public InsuranceReminderBO() { } #region Objects and Properties private int id; private String strCompanyName; private String strPremiumAmount; private String stDueDate; public int ID { get { return this.id; } set { this.id = value; } } public String Company_Name { get { return this.strCompanyName; } set { this.strCompanyName = value; } } public String Premium_Amount { get { return this.strPremiumAmount; } set { this.strPremiumAmount = value; } } public String Due_Date { get { return this.stDueDate; } set { this.stDueDate = value; } } #endregion #region IParcelable implementation // The creator creates an instance of the specified object private static readonly GenericParcelableCreator&lt;InsuranceReminderBO&gt; _creator = new GenericParcelableCreator&lt;InsuranceReminderBO&gt;((parcel) =&gt; new InsuranceReminderBO(parcel)); [ExportField("CREATOR")] public static GenericParcelableCreator&lt;InsuranceReminderBO&gt; GetCreator() { return _creator; } // Create a new SelectListItem populated with the values in parcel private InsuranceReminderBO(Parcel parcel) { ID = parcel.ReadInt(); Company_Name = parcel.ReadString(); Premium_Amount = parcel.ReadString(); Due_Date = parcel.ReadString(); } public int DescribeContents() { return 0; } // Save this instance's values to the parcel public void WriteToParcel(Parcel dest, ParcelableWriteFlags flags) { dest.WriteInt(ID); dest.WriteString(Company_Name); dest.WriteString(Premium_Amount); dest.WriteString(Due_Date); } // Closest to the 'Java' way of implementing the creator /*public sealed class SelectListItemCreator : Java.Lang.Object, IParcelableCreator { public Java.Lang.Object CreateFromParcel(Parcel source) { return new SelectListItem(source); } public Java.Lang.Object[] NewArray(int size) { return new SelectListItem[size]; } }*/ #endregion } #region GenericParcelableCreator /// &lt;summary&gt; /// Generic Parcelable creator that can be used to create objects from parcels /// &lt;/summary&gt; public sealed class GenericParcelableCreator&lt;T&gt; : Java.Lang.Object, IParcelableCreator where T : Java.Lang.Object, new() { private readonly Func&lt;Parcel, T&gt; _createFunc; /// &lt;summary&gt; /// Initializes a new instance of the &lt;see cref="ParcelableDemo.GenericParcelableCreator`1"/&gt; class. /// &lt;/summary&gt; /// &lt;param name='createFromParcelFunc'&gt; /// Func that creates an instance of T, populated with the values from the parcel parameter /// &lt;/param&gt; public GenericParcelableCreator(Func&lt;Parcel, T&gt; createFromParcelFunc) { _createFunc = createFromParcelFunc; } #region IParcelableCreator Implementation public Java.Lang.Object CreateFromParcel(Parcel source) { return _createFunc(source); } public Java.Lang.Object[] NewArray(int size) { return new T[size]; } #endregion } #endregion' </code></pre> <p>I am putting object in intent as </p> <pre><code>InsuranceReminderBO selectedInsurance = listOfInsurance[e.Position]; Intent intent = new Intent(this, typeof(ReminderDetails)); intent.PutExtra("SelectedItem", selectedInsurance); </code></pre> <p>And reading in second activity as</p> <pre><code>InsuranceReminderBO i = (InsuranceReminderBO)Intent.GetParcelableExtra("SelectedItem"); </code></pre> <p>but getting i as null.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload