简体   繁体   English

如何使用Xamarin在Android中通过意图将列表从一个活动传递到另一个活动

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

i am new to Xamarin. 我是Xamarin的新手。 I need to pass List of data as from one activity to another activity via intent .and get those data in another activity screen and process those data's. 我需要通过意图将数据列表从一个活动传递到另一个活动,然后在另一个活动屏幕中获取这些数据并处理这些数据。 Please suggest a solution. 请提出解决方案。

Thanks in advance 提前致谢

Use IList of generic type to pass data to the required data from one activity to another. 使用通用类型的IList将数据从一个活动传递到所需数据到另一个活动。

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

then pass this List in the intent 然后有意通过此列表

        i.PutStringArrayListExtra("month_year",Mon_year);

In Another activity(where you want to get the sent data) 在另一个活动中(您要在其中获取发送的数据)

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

I approached this a bit differently. 我对此的处理方式有所不同。 I created a base class which inherited from Java.Lang.Object, ISerializable so that I could use Bundle.PutSerializable(string key, ISerializable value) to pass my objects between Activities. 我创建了一个继承自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));
    }
}

I can then pass a list of Person objects to a new Activity by doing: 然后,我可以通过执行以下操作将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);

Then Recieve the objects like so: 然后像这样接收对象:

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);
}

For passing a list of content from one activity to other activity you can use parcelable class. 要将内容列表从一个活动传递到另一个活动,可以使用可打包类。 Its a type of bundle in which we can pass any type of content from one activity to other activity. 它是一种包,其中我们可以将任何类型的内容从一个活动传递到另一个活动。 The only thing is you just need to customize the parcelable class according to your need. 唯一的事情就是您只需要根据需要自定义可打包类。

Please visit this Link or download this sample project so that you can understand more about passing list of content from one activity to other activity. 请访问此链接或下载此示例项目,以便您更多地了解将内容列表从一个活动传递到另一活动的更多信息。

Hope this will solve your problem. 希望这能解决您的问题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何通过android中的意图将字典从一个活动传递到另一个活动 - how to pass dictionary from one activity to another activity via intent in android 将 Intent 从一个活动传递到另一个活动 - Pass Intent from one activity to another activity 如何使用 android 中的 Intent 将 ArrayList 个对象从一个活动传递到另一个活动? - How to pass ArrayList of Objects from one to another activity using Intent in android? 如何在Android中使用Intent将图片从一个活动发送到另一个活动 - How to send Images one Activity to another Activity using Intent in android 如何使用Xamarin Android中的Intent将图片从一个活动传递到另一个活动? - How to pass an image from one activity to another using Intents in Xamarin Android? 如何在不使用Intent的情况下将对象从一个活动传递到另一个活动 - How to pass object from one activity to another activity without using Intent 如何传递ArrayList <File> 对象从一个活动到另一个活动使用意图? - How to pass ArrayList<File> object from one Activity to another Activity using Intent? 无法通过意图将数组列表从一个活动传递到另一个活动 - Unable to pass array list from one activity to another through intent 将ImageView从一项活动传递到另一项活动-Intent-Android - Pass ImageView from one activity to another - Intent - Android 如何仅使用点击事件和意图将当前日期从一个活动传递到android中的下一个活动 - How to Pass Current Date from One activity to next activity in android only using click event and intent
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM