Xamarin.android-将数据传递到我的片段

问题描述 投票:0回答:1

我有一个包含我的服务的列表,并且在选择的项目上,我正在将服务类型传递给我的活动ServiceDetail,如下所示:

服务活动

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

我不想为每个服务创建一个活动,我只想拥有一个“服务活动详细信息”,并按服务类型自定义文本和图像。

错误:当我选择项目服务时:

System.NullReferenceException - Object reference not set to an instance of an object. on: 
var test = Arguments.GetString("text");
android android-fragments xamarin xamarin.android
1个回答
3
投票

您有两种方法可以做到这一点。

如果那是保存片段的活动,则可以调用

this.Activity

内部片段,并在投射后调用该活动的任何方法

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

或者您可以通过使用委托来做到这一点:

在Fragment类中定义委托

namespace Awesome.Android {
     public class AwesomeFragment : Fragment {

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

创建Framgent时可以分配它

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

之后,在活动中实现OnAwesomePress

private void OnAwesomePress (int number) {

}

现在,当您在Fragment中调用sendOnAwesomePressEvent时,该事件将传递给Activity。

sendOnAwesomePressEvent (10);
© www.soinside.com 2019 - 2024. All rights reserved.