C# Xamarin Android | Change Contentview from class

26 Views Asked by At

I am adding my button and the click event from a class, When you click the button you need to go to a different content view and bring a variable with it.

protected override void OnPostExecute(Java.Lang.Object result)
{
    base.OnPostExecute(result);

    pd.Dismiss();

    if((bool)result)
    {
        lv.Adapter = new CustomAdapter(c, meters);
        lv.ItemClick += Lv_ItemClick;
    }
    else
    {
        Toast.MakeText(c,"Unable To Parse",ToastLength.Short).Show();
    }
}

private void Lv_ItemClick(object sender, AdapterView.ItemClickEventArgs e)
{
   Toast.MakeText(c, meters[e.Position].EAN, ToastLength.Short).Show();          
}

The Toast displays the data that should be shown in the next content view.

SetContentView doesn't work from a class. SetContentView(Resource.Layout.address_data);

How do I change content view from a class? and how would I get the data to the new view?

SetContentView(Resource.Layout.address_data);

1

There are 1 best solutions below

0
Siebe Van Hirtum On

I found the solution:

Activity activity = c as Activity;
activity.SetContentView(Resource.Layout.address_data);

For giving the data with the new view I used:

Activity activity = c as Activity;
if (activity != null)
{
    Intent intent = new Intent(activity, typeof(AddressData));
    intent.PutExtra("EAN", meters[e.Position].EAN);
    activity.StartActivity(intent);
}