0

i am new to 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

Rameshbabu
  • 405
  • 2
  • 7
  • 17
  • You can check this http://stackoverflow.com/questions/13633092/efficiently-passing-custom-object-data-between-android-activities-mono-android – Piyush Nov 13 '14 at 06:24
  • [Have a look here](http://androidideasblog.blogspot.in/2010/02/passing-list-of-objects-between.html) – SweetWisher ツ Nov 13 '14 at 06:58

3 Answers3

2

Use IList of generic type to pass data to the required data from one activity to another.

        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... 
Rameshbabu
  • 405
  • 2
  • 7
  • 17
1

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.

[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:

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

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.

Sreedev
  • 6,563
  • 5
  • 43
  • 66