Skip to content

Instantly share code, notes, and snippets.

@gavilanch
Created August 26, 2017 21:36
Show Gist options
  • Save gavilanch/43dc9cf22990803922db80c16667ee29 to your computer and use it in GitHub Desktop.
Save gavilanch/43dc9cf22990803922db80c16667ee29 to your computer and use it in GitHub Desktop.
// MainActivity.cs
internal static string KEY_ID = "KEY_ID";
private News _news;
private readonly string KEY_BODY = "KEY_BODY";
private readonly string KEY_IMAGE_NAME = "KEY_IMAGE_NAME";
private readonly string KEY_TITLE = "KEY_TITLE";
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
// Set our view from the "main" layout resource
SetContentView (Resource.Layout.Main);
PrepareActionBar();
var id = Intent.Extras.GetInt(KEY_ID);
if (bundle == null)
{
var newsService = new NewsService();
_news = newsService.GetNewsById(id);
}
else
{
_news = new News();
_news.Id = bundle.GetInt(KEY_ID);
_news.Body = bundle.GetString(KEY_BODY);
_news.ImageName = bundle.GetString(KEY_IMAGE_NAME);
_news.Title = bundle.GetString(KEY_TITLE);
}
var newsTitle = FindViewById<TextView>(Resource.Id.newsTitle);
var newsBody = FindViewById<TextView>(Resource.Id.newsBody);
var newsImage = FindViewById<ImageView>(Resource.Id.newsImage);
var display = WindowManager.DefaultDisplay;
Android.Graphics.Point point = new Android.Graphics.Point();
display.GetSize(point);
var imageURL = string.Concat(ValuesService.ImagesBaseURL,
_news.ImageName);
Picasso.With(ApplicationContext)
.Load(imageURL)
.Resize(point.X, 0)
.Into(newsImage);
newsTitle.Text = _news.Title;
newsBody.Text = _news.Body;
}
protected override void OnSaveInstanceState(Bundle outState)
{
outState.PutString(KEY_BODY, _news.Body);
outState.PutInt(KEY_ID, _news.Id);
outState.PutString(KEY_IMAGE_NAME, _news.ImageName);
outState.PutString(KEY_TITLE, _news.Title);
base.OnSaveInstanceState(outState);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment