1

我正在使用 monodroid 来制作应用程序。在应用程序中,用户单击进入列表视图的按钮,该列表视图有多个选项,用户选择选项,然后该选项被发送回原始类,其中文本视图更改为与选项相同的文本被选中的列表视图。

下面是发送数据的类

 protected override void OnListItemClick(ListView l, View v, int position, long id)
    {
        int t = labels[position];
        String text = t + "";

        Intent send = new Intent(this, typeof(CreateVehicle));
        send.SetFlags(ActivityFlags.ClearTop | ActivityFlags.SingleTop);
        send.PutExtra("t", "Data from FirstActivity");
        StartActivity(send);

    }

下面是接收类:

protected override void OnResume()
    {
        base.OnResume(); // Always call the superclass first.

        TextView tvYearChange = FindViewById<TextView>(Resource.Id.tvYearchange);

        string text = Intent.GetStringExtra("t") ?? "work";

        tvYearChange.SetText(text, null);

    }

如果有人能弄清楚为什么我没有收到很棒的数据。

4

2 回答 2

0

我不确定我是否正确解决了您的问题,但我猜您的 Main 活动正在调用具有 ListView 的活动,然后您希望将结果返回到 Main 活动中,对吗?

如果是这样,您显示的代码不是做您想做的事情的正确方法。您正在寻找的是在您的主要活动中使用StartActivityForResult和覆盖该方法。onActivityResult

我不确切知道如何使用 Monodroid 和 C#,但我可以给你一个 Java 示例,我相信它会帮助你理解如何获得你想要的东西:

假设我的 ListView 活动称为myList并扩展了 ListActivity ,我的主要活动称为MainActivity。下面是 myList 的 OnListItemClick方法

@Override
protected void onListItemClick(ListView l, View v, int position, long id){
    super.onListItemClick(l, v, position, id);

    String text = String.valueOf(labels[position]);

    // Create the intent with the extras
    Intent send = new Intent();
    send.putExtra("text_from_list", text);

    // Set the result to OK (meaning the extras are put as expected by the caller)
    setResult(RESULT_OK, send);
      // you need this so the caller (MainActivity) knows that the user completed
      // this activity (myList) as expected (clicking on the item to return a result)
      // and didn't just leave the activity (back button)

    // Close this List Activity
    finish();
}

下面是我的Main Activity中调用 myList 的方法:

private void callListActivity(){
    Intent call = new Intent(MainActivity.this, myList.class);
    startActivityForResult(call, 1);    
      // The second field is just a number to identify which activity 
      // returned a result when a result is received (you can have 
      // several calls to different activities expecting results from
      // each one of them). This number is called requestCode.
}

您将不得不使用以下内容覆盖MainActivityonActivityResult中的 :

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    // Check if the returned data came from myList (requestCode 1) and if
    // data was actually received (check if resultCode = RESULT_OK)
    if (requestCode == 1) { 
        if (resultCode == RESULT_OK) {
            String text = data.getStringExtra("text_from_list");
              // you have to use the same string identifier you used
              // when you put the extra in the Intent in myList

            TextView tvYearChange = (TextView)findViewById(R.id.tvYearchange);

            tvYearChange.setText(text);
        }
        else {
            // Do something if the Activity didn't return data
            // (resultCode != RESULT_OK)
        }
    }

    // If you are expecting results from other Activities put more if
    // clauses here with the appropriate request code, for example:
    // if (requestCode == 2) {
    //     DoSomething;
    // }
    // And so on...
}

将此 Java 代码修改为可与 Monodroid 一起使用的 C# 代码应该不难。另外,看看Android 官方文档中的这个链接,那里有很多有用的东西。

我希望这可以帮助你。

于 2013-07-23T19:01:10.673 回答
0

在 Receiver Activity 中从 Intent 获取数据,如下所示

Intent intent = getIntent();
string text = intent.getStringExtra("t");
于 2015-12-16T10:17:41.727 回答