簡體   English   中英

如何使用Xamarin在Android中通過意圖將列表從一個活動傳遞到另一個活動

[英]How to pass List via intent from one activity to another activity in android using xamarin

我是Xamarin的新手。 我需要通過意圖將數據列表從一個活動傳遞到另一個活動,然后在另一個活動屏幕中獲取這些數據並處理這些數據。 請提出解決方案。

提前致謝

使用通用類型的IList將數據從一個活動傳遞到所需數據到另一個活動。

        IList<String> Mon_year = new List<String>();

然后有意通過此列表

        i.PutStringArrayListExtra("month_year",Mon_year);

在另一個活動中(您要在其中獲取發送的數據)

        IList<String> Mon_year = Intent.GetStringArrayListExtra("month_year") ;// here u get the Ilist of String data... 

我對此的處理方式有所不同。 我創建了一個繼承自Java.Lang.Object, ISerializable的基類Java.Lang.Object, ISerializable以便可以使用Bundle.PutSerializable(string key, ISerializable value)在Activity之間傳遞對象。

[Flags]
public enum ObjectState
{
    Normal = 0,
    New = 1,
    Modified = 2,
    Removed = 4
}

public abstract class ObjectBase : Java.Lang.Object, ISerializable
{
    public Guid Id { get; set; }

    public ObjectState State { get; set; }

    protected ObjectBase(IntPtr handle, JniHandleOwnership transfer) : base(handle, transfer)
    {
    }

    protected ObjectBase()
    {
    }

    [Export("readObject", Throws = new[] { typeof(IOException), typeof(Java.Lang.ClassNotFoundException) })]
    private void ReadObject(ObjectInputStream stream)
    {
        this.Deserialize(stream);
    }

    [Export("writeObject", Throws = new[] { typeof(IOException), typeof(Java.Lang.ClassNotFoundException) })]
    private void WriteObject(ObjectOutputStream stream)
    {
        this.Serialize(stream);
    }

    protected virtual void Deserialize(ObjectInputStream stream)
    {
        this.Id = Guid.Parse(stream.ReadUTF());
        this.State = (ObjectState)stream.ReadInt();
    }

    protected virtual void Serialize(ObjectOutputStream stream)
    {
        stream.WriteUTF(this.Id.ToString());
        stream.WriteInt((int)this.State);
    }
}

public class Person : ObjectBase
{
    public string Name { get; set; }

    public int Age { get; set; }

    public bool IsMarried { get; set; }

    public DateTime? Anniversary { get; set; }

    protected override void Deserialize(ObjectInputStream stream)
    {
        base.Deserialize(stream);

        if (stream.ReadBoolean())
            this.Name = stream.ReadUTF();

        this.Age = stream.ReadInt();

        this.IsMarried = stream.ReadBoolean();

        if (stream.ReadBoolean())
            this.Anniversary = DateTime.Parse(stream.ReadUTF());
    }

    protected override void Serialize(ObjectOutputStream stream)
    {
        base.Serialize(stream);

        stream.WriteBoolean(this.Name != null);

        if (this.Name != null)
            stream.WriteUTF(this.Name);

        stream.WriteInt(this.Age);

        stream.WriteBoolean(this.IsMarried);

        stream.WriteBoolean(this.Anniversary != null);

        if (this.Anniversary != null)
            stream.WriteUTF(this.Anniversary.Value.ToString(CultureInfo.InvariantCulture));
    }
}

然后,我可以通過執行以下操作將Person對象的列表傳遞給新的Activity:

List<Person> persons = new List<Person>();
var intent = new Intent(this, typeof(MyActivity));
var bundle = new Bundle();
for (int i = 0; i < persons.Count; i++)
{
    var p = persons[i];
    bundle.PutSerializable("Person" + i, p);
}
intent.PutExtras(bundle);
this.StartActivity(intent);

然后像這樣接收對象:

protected override void OnCreate(Bundle bundle)
{
    List<Person> persons = new List<Person>();
    int i = 0;
    while (bundle.ContainsKey("Person" + i))
    {
        var p = (Person) bundle.GetSerializable("Person" + i);
        persons.Add(p);
        i++;
    }
    base.OnCreate(bundle);
}

要將內容列表從一個活動傳遞到另一個活動,可以使用可打包類。 它是一種包,其中我們可以將任何類型的內容從一個活動傳遞到另一個活動。 唯一的事情就是您只需要根據需要自定義可打包類。

請訪問此鏈接或下載此示例項目,以便您更多地了解將內容列表從一個活動傳遞到另一活動的更多信息。

希望這能解決您的問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM