在 C# 中的 Android 活动之间传递自定义对象 [英] Passing custom object between Android activities in C#
问题描述
我正在使用 Xamarin.Android 在 VS2012 中创建一个 android 应用程序.我在主屏幕中显示自定义列表.当用户单击列表项时,我需要将一个自定义对象(带有 ID、字符串、字符串、字符串属性)从这个 Main 活动传递给另一个.
谁能帮我举个例子?
我已经尝试过在其他问题中提到的解决方案>
但问题是我遇到了以下异常:
这就是我在第二个活动中提取
InsuranceReminderBO i = (InsuranceReminderBO)Intent.GetSerializableExtra("SelectedItemID");
i 为空
并且在第一个活动设置中是这样的:
Intent intent = new Intent(this, typeof(ReminderDetails));intent.PutExtra("SelectedItemID", selectedInsurance);StartActivity(typeof(ReminderDetails));
其中 class InsuranceReminderBO
定义为
public class InsuranceReminderBO : Java.Lang.Object, Java.IO.ISerializable
我也尝试过使用 IParcelable
但我得到了错误 Creator is not defined在 ICreator 或 Creator 中
遵循 Iparcelable 在 CustomObject 上的实现
'public class InsuranceReminderBO : Java.Lang.Object, IParcelable{公共保险ReminderBO(){}#region 对象和属性私有整数 ID;私人字符串 strCompanyName;私人字符串 strPremiumAmount;私人字符串标准日期;公共整数 ID{得到 { 返回 this.id;}设置 { this.id = 值;}}公共字符串 Company_Name{得到 { 返回 this.strCompanyName;}set { this.strCompanyName = value;}}公共字符串 Premium_Amount{得到 { 返回 this.strPremiumAmount;}设置 { this.strPremiumAmount = value;}}公共字符串 Due_Date{得到 { 返回 this.stDueDate;}设置 { this.stDueDate = 值;}}#endregion#region IParcelable 实现//创建者创建指定对象的实例私有静态只读 GenericParcelableCreator_创作者= new GenericParcelableCreator((parcel) => new InsuranceReminderBO(parcel));[导出字段(创建者")]public static GenericParcelableCreator获取创建者(){返回_creator;}//创建一个新的 SelectListItem,其中填充了 Parcel 中的值私人保险ReminderBO(包裹包裹){ID = Parcel.ReadInt();Company_Name = parcel.ReadString();Premium_Amount = parcel.ReadString();Due_Date = parcel.ReadString();}public int DescribeContents(){返回0;}//将此实例的值保存到parcel公共无效WriteToParcel(Parcel dest,ParcelableWriteFlags标志){dest.WriteInt(ID);dest.WriteString(Company_Name);dest.WriteString(Premium_Amount);dest.WriteString(Due_Date);}//最接近实现创建者的Java"方式/* 公共密封类 SelectListItemCreator : Java.Lang.Object, IParcelableCreator{public Java.Lang.Object CreateFromParcel(Parcel source){返回新的 SelectListItem(source);}public Java.Lang.Object[] NewArray(int size){返回新的 SelectListItem[size];}}*/#endregion}#region GenericParcelableCreator///<总结>///通用 Parcelable 创建器,可用于从包裹创建对象///</总结>公共密封类 GenericParcelableCreator: Java.Lang.Object, IParcelableCreator其中 T : Java.Lang.Object, new(){private readonly Func<Parcel, T>_createFunc;///<总结>///初始化 <see cref="ParcelableDemo.GenericParcelableCreator`1"/> 的新实例班级.///</总结>//////创建 T 实例的 Func,填充来自parcel 参数的值///</param>公共 GenericParcelableCreator(Func createFromParcelFunc){_createFunc = createFromParcelFunc;}#region IParcelableCreator 实现public Java.Lang.Object CreateFromParcel(Parcel source){返回 _createFunc(source);}public Java.Lang.Object[] NewArray(int size){返回新的 T[size];}#endregion}#endregion'
我把对象放在意图中
InsuranceReminderBO selectedInsurance = listOfInsurance[e.Position];Intent intent = new Intent(this, typeof(ReminderDetails));intent.PutExtra("SelectedItem", selectedInsurance);
在第二个活动中阅读
InsuranceReminderBO i = (InsuranceReminderBO)Intent.GetParcelableExtra("SelectedItem");
但让 i 为空.
在 Google 上做了大量搜索后,我终于找到了解决方案 此处.
基本上我使用了 StartActivity(intent);
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.
Can anyone please help me with some example?
edit:
I have already tried solution mention in other question
but the problem is I am getting below exception:
This is how I am extracting in second activity
InsuranceReminderBO i = (InsuranceReminderBO)Intent.GetSerializableExtra("SelectedItemID");
i is null
and in first activity setting it like this:
Intent intent = new Intent(this, typeof(ReminderDetails));
intent.PutExtra("SelectedItemID", selectedInsurance);
StartActivity(typeof(ReminderDetails));
where class InsuranceReminderBO
is defined as
public class InsuranceReminderBO : Java.Lang.Object, Java.IO.ISerializable
I have also tried using IParcelable
but in that I got error Creator is not defined
in ICreator or Creator
Following the implementation of Iparcelable on CustomObject
'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<InsuranceReminderBO> _creator
= new GenericParcelableCreator<InsuranceReminderBO>((parcel) => new InsuranceReminderBO(parcel));
[ExportField("CREATOR")]
public static GenericParcelableCreator<InsuranceReminderBO> 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
/// <summary>
/// Generic Parcelable creator that can be used to create objects from parcels
/// </summary>
public sealed class GenericParcelableCreator<T> : Java.Lang.Object, IParcelableCreator
where T : Java.Lang.Object, new()
{
private readonly Func<Parcel, T> _createFunc;
/// <summary>
/// Initializes a new instance of the <see cref="ParcelableDemo.GenericParcelableCreator`1"/> class.
/// </summary>
/// <param name='createFromParcelFunc'>
/// Func that creates an instance of T, populated with the values from the parcel parameter
/// </param>
public GenericParcelableCreator(Func<Parcel, T> 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'
I am putting object in intent as
InsuranceReminderBO selectedInsurance = listOfInsurance[e.Position];
Intent intent = new Intent(this, typeof(ReminderDetails));
intent.PutExtra("SelectedItem", selectedInsurance);
And reading in second activity as
InsuranceReminderBO i = (InsuranceReminderBO)Intent.GetParcelableExtra("SelectedItem");
but getting i as null.
After doing a lot of search on Google finally i found a solution here.
Basically I used StartActivity(intent);
这篇关于在 C# 中的 Android 活动之间传递自定义对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!