Diego Cespedes
Diego Cespedes

Reputation: 1353

Xamarin.android - Pass data to my fragment

i have a List with my services, and on item selected i'm passing service type to my activity ServiceDetail like this:

ServiceActivity

void item_selected(object sender, AdapterView.ItemClickEventArgs e) {
    MenuContentItem selectedItem = (MenuContentItem)item[e.Position];
    if(selectedItem.Title == "COLLO") {
        var activity_go = new Intent(this, typeof(ServiceDetailActivity));
        activity_go.PutExtra("service_type", "Collo");
        StartActivity(activity_go);
    }
    if (selectedItem.Title == "SPALLA") {
        var activity_go = new Intent(this, typeof(ServiceDetailActivity));
        activity_go.PutExtra("service_type", "Spalla");
        StartActivity(activity_go);
    }
}

ServiceDetailActivity

protected override void OnCreate(Bundle savedInstanceState) {
    base.OnCreate(savedInstanceState);
    SetContentView(Resource.Layout.ServiceDetail);
    //enable navigation mode to support tab layout
    this.ActionBar.NavigationMode = ActionBarNavigationMode.Tabs;
    AddTab("Introduzione", Resource.Mipmap.Icon, new IntroduzioneFragment());
    //intent data 
    string text = Intent.GetStringExtra("service_type") ?? "Data not available";
    IntroduzioneFragment fragment = new IntroduzioneFragment();
    // set data to pass to my fragment
    Bundle bundle = new Bundle();
    bundle.PutString("text", text);
    fragment.Arguments = bundle;
}
// MY FRAGMENT - I would like "CUSTOM" my fragment "IntroduzioneFragment" like this:
class IntroduzioneFragment : Fragment {
    public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        base.OnCreateView(inflater, container, savedInstanceState);
        var view = inflater.Inflate(Resource.Menu.Tab, container, false);
        var sampleTextView = view.FindViewById<TextView>(Resource.Id.textView);
        var imageView = view.FindViewById<ImageView>(Resource.Id.image_view);
        imageView.SetImageResource(Resource.Mipmap.slide1);
        // Get type of service
        var test = Arguments.GetString("text");
        if (test == "Collo") {
            sampleTextView.Text = "is collooooo";
        } else {
            sampleTextView.Text = "is not collo";
        }
        return view;
    }
}

I don't want create one activity for each service, i would like just have one "Service activity detail" and custom text and image by service type.

ERROR: when I select item service:

System.NullReferenceException - Object reference not set to an instance of an object. on: 
var test = Arguments.GetString("text");

Upvotes: 0

Views: 1240

Answers (1)

Taier
Taier

Reputation: 2119

You have two ways of doing that.

If that is the activity that holds the fragment, you can call

this.Activity

inside fragment and just call any method of the activity after casting

AwesomceActivty castetActivity = (AwesomeActivity)this.Activity;
castetActivity.AwesomeMethod(12);

Or you can do that by using Delegates:

Define delegates in your Fragment class

namespace Awesome.Android {
     public class AwesomeFragment : Fragment {

       public delegate void OnAwesomePress (int number);
       public event OnAwesomePress sendOnAwesomePressEvent;
     }
}

You can assign it when you create a Framgent

AwesomeFragment fragment = new AwesomeFragment ();
fragment.OnAwesomePress += OnAwesomePress;

After that, you implement OnAwesomePress in your activity

private void OnAwesomePress (int number) {

}

Now, when you call sendOnAwesomePressEvent in your Fragment, that event will be passed to Activity.

sendOnAwesomePressEvent (10);

Upvotes: 3

Related Questions