Created
January 23, 2016 13:59
-
-
Save nglauber/f556da48315ef755b638 to your computer and use it in GitHub Desktop.
Passing Parcelable objects through Activities using Xamarin
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using Android.App; | |
using Android.Content; | |
using Android.OS; | |
using Android.Runtime; | |
using Android.Views; | |
using Android.Widget; | |
using Square.Picasso; | |
using Newtonsoft.Json; | |
namespace BookStore_Android | |
{ | |
[Activity (Label = "BookDetailActivity")] | |
public class BookDetailActivity : Activity | |
{ | |
protected override void OnCreate (Bundle savedInstanceState) | |
{ | |
base.OnCreate (savedInstanceState); | |
SetContentView (Resource.Layout.BookDetail); | |
TextView txt = FindViewById<TextView> (Resource.Id.txtTitulo); | |
ImageView img = FindViewById<ImageView> (Resource.Id.imgCapa); | |
//Book book = JsonConvert.DeserializeObject<Book>(Intent.GetStringExtra("book")); | |
Book book = Intent.GetParcelableExtra("book") as Book; | |
//Book book = Intent.GetSerializableExtra("book") as Book; | |
txt.Text = book.Name; | |
Picasso.With(this) | |
.Load(book.CoverPath) | |
.Into(img); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Android.App; | |
using Android.Widget; | |
using Android.OS; | |
using Android.Content; | |
using System.Collections.Generic; | |
using Newtonsoft.Json; | |
using Java.Lang; | |
using Java.IO; | |
using Java.Interop; | |
namespace BookStore_Android | |
{ | |
[Activity (Label = "BookStore_Android", MainLauncher = true, Icon = "@mipmap/icon")] | |
public class MainActivity : ListActivity | |
{ | |
List<Book> books; | |
protected override void OnCreate (Bundle savedInstanceState) | |
{ | |
base.OnCreate (savedInstanceState); | |
books = new List<Book>(); | |
books.Add (new Book () { | |
Author = "Nelson Glauber", | |
Name = "Dominando o Android ", //+ i, | |
Editor = "Novatec", | |
Year = 2015, | |
CoverPath = "http://www.novatec.com.br/livros/dominando-android-2ed/capa_ampliada9788575224632.jpg" | |
}); | |
books.Add (new Book () { | |
Author = "Dan Hermes", | |
Name = "Desenvolvimento de Aplicativos Móveis com Xamarin ", //+ i, | |
Editor = "Apress", | |
Year = 2015, | |
CoverPath = "http://www.novatec.com.br/livros/desenvolvimento-aplicativos-xamarin/capa_ampliada9788575224663.jpg" | |
}); | |
// var listAdapter = new ArrayAdapter (this, Android.Resource.Layout.SimpleListItem1, books); | |
var listAdapter = new BookAdapter (this, books); | |
ListAdapter = listAdapter; | |
} | |
protected override void OnListItemClick (ListView l, Android.Views.View v, int position, long id) | |
{ | |
base.OnListItemClick (l, v, position, id); | |
Intent it = new Intent(this, typeof(BookDetailActivity)); | |
// it.PutExtra ("book", JsonConvert.SerializeObject(books [position])); | |
it.PutExtra ("book", books [position]); | |
StartActivity (it); | |
} | |
} | |
public class Book : Object, IParcelable { | |
public string Name; | |
public string Author; | |
public string Editor; | |
public int Year; | |
public string CoverPath; | |
public override string ToString () | |
{ | |
return string.Format ("{0} - {1}", Name, Author); | |
} | |
[ExportField ("CREATOR")] // Need a reference to Mono.Android.Export | |
public static BookCreator InitializeCreator () | |
{ | |
return new BookCreator (); | |
} | |
public void WriteToParcel (Parcel dest, ParcelableWriteFlags flags) | |
{ | |
dest.WriteString (Name); | |
dest.WriteString (Author); | |
dest.WriteString (Editor); | |
dest.WriteInt (Year); | |
dest.WriteString (CoverPath); | |
} | |
public int DescribeContents () | |
{ | |
return 0; | |
} | |
} | |
public class BookCreator : Object, IParcelableCreator | |
{ | |
public Object CreateFromParcel (Parcel source) | |
{ | |
Book book = new Book (); | |
book.Name = source.ReadString (); | |
book.Author = source.ReadString (); | |
book.Editor = source.ReadString (); | |
book.Year = source.ReadInt (); | |
book.CoverPath = source.ReadString (); | |
return book; | |
} | |
public Object [] NewArray (int size) | |
{ | |
return new Object [size]; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment