将数据从片段发送到 activity xamarin android

send data from a fragment to an activity xamarin android

抱歉,我正在使用 google 翻译。

我试图将数据从 Fragment 发送到 Activity 但没有成功,这样做的目的是更新 TextView。我无法验证 Intent 是否带来了数据,如果我消除了它们具有空值的验证,我将详细说明我的代码。

片段

private void sendData()
 {
    Intent i = new Intent(Context,Activity.Class);
    //PACK DATA
    i.PutExtra("COUNT", "1");
    //START ACTIVITY
    Activity.StartActivity(i);
}

已调用

void ButtonClicked2(object sender, EventArgs args)
    {
        sendData();
    }

Activity

protected override void OnResume()
    {
        base.OnResume();
        //Intent i = Intent;
        Bundle sender = Intent.Extras;

        ////IF ITS THE FRAGMENT THEN RECEIVE DATA
        if (sender != null)
        {
            receiveData();
            Android.Widget.Toast.MakeText(this, "Received", Android.Widget.ToastLength.Short).Show();

        }
       
    }
    private void receiveData()
    {
        //RECEIVE DATA VIA INTENT
        Intent i =  Intent;
       string count = i.GetStringExtra("COUNT");
       
        //SET DATA TO TEXTVIEWS
        Android.Widget.TextView CountView = FindViewById<Android.Widget.TextView>(Resource.Id.itemCount);
        CountView.SetText(count, Android.Widget.TextView.BufferType.Normal);
        Android.Widget.Toast.MakeText(this, count, Android.Widget.ToastLength.Short).Show();
    }

一个关于如何将数据从fragement传递到Activity的简单例子,供大家参考。

片段:

我的片段:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
    android:id="@+id/textView1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"/>
<Button
    android:id="@+id/button1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"/>
</LinearLayout>

MyFragement.cs:

public class MyFragment : Fragment
{
     
    public override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);

        // Create your fragment here
    }

    
    public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
        // Use this to return your custom view for this Fragment
        // return inflater.Inflate(Resource.Layout.YourFragment, container, false);

        // A large amount of text to display.
        var textToDisplay = "Hello";

        var view = inflater.Inflate(Resource.Layout.MyFragment, container, false);

        var textView = view.FindViewById<TextView>(Resource.Id.textView1);
        textView.Text = textToDisplay;

        var button = view.FindViewById<Button>(Resource.Id.button1);
        button.Click += Button_Click;           

        return view;
    }

    private void Button_Click(object sender, EventArgs e)
    {
        sendData();
    }
    private void sendData()
    {
        Intent i = new Intent(Context, Activity.Class);
        //PACK DATA
        i.PutExtra("COUNT", "1");
        //START ACTIVITY
        Activity.StartActivity(i);
    }
}

Activity:

activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/fragment_container"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>

MainAcitivity.cs:

  public class MainActivity : AppCompatActivity
{
    protected override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);
        Xamarin.Essentials.Platform.Init(this, savedInstanceState);
        // Set our view from the "main" layout resource
        SetContentView(Resource.Layout.activity_main);
    
        var newFragment = new MyFragment();
        var ft = FragmentManager.BeginTransaction();
        ft.Add(Resource.Id.fragment_container, newFragment);
        ft.Commit();

    }
    protected override void OnResume()
    {
        base.OnResume();
        //Intent i = Intent;
        Bundle sender = Intent.Extras;

        ////IF ITS THE FRAGMENT THEN RECEIVE DATA
        if (sender != null)
        {
            receiveData();
            Android.Widget.Toast.MakeText(this, "Received", Android.Widget.ToastLength.Short).Show();

        }

    }
    private void receiveData()
    {
        //RECEIVE DATA VIA INTENT
        Intent i = Intent;
        string count = i.GetStringExtra("COUNT");             

        var textView =  FindViewById<Android.Widget.TextView>(Resource.Id.textView1);
        textView.Text = count;

        //SET DATA TO TEXTVIEWS
        //Android.Widget.TextView CountView = FindViewById<Android.Widget.TextView>(Resource.Id.itemCount);
        //CountView.SetText(count, Android.Widget.TextView.BufferType.Normal);
        //Android.Widget.Toast.MakeText(this, count, Android.Widget.ToastLength.Short).Show();
    }

}

截图:

我能够自己解决它,虽然我的解决方案不能完全说服我,但我会post它以防有人遇到与我相同的问题。

片段

//add the activity reference to the fragment
using static GumisaAPP.Activities.BaseActivity;


//we create the procedure that will be in charge of modifying the activity's TextView
private void sendData_CountItem()
 {
        string count;
        Variables.Contador_Items = Variables.Contador_Items + 1;
        count= Convert.ToString(Variables.Contador_Items);
//we access the texview of the activity from the fragment
        TextView textCount = Activity.FindViewById<TextView>(Resource.Id.itemCount);
// modify the text of the TextView
        textCount.SetText(count, TextView.BufferType.Normal);
 }

// call the procedure
private void EnviarDatos(){
sendData_CountItem();
}

感谢您不厌其烦地回答我的问题。问候