簡體   English   中英

無法從意圖接收數據

[英]Can't receive data from intent

我正在使用monodroid進行申請。 在應用程序中,用戶單擊進入列表視圖的按鈕,該列表視圖具有多個選項,用戶選擇on選項,然后將該選項發送回原始類,在該類中,textview更改為與文本視圖上的選項相同的文本。選定的列表視圖。

下面是發送數據的類

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

    }

如果有人能弄清楚為什么我沒有收到數據,那將是很好的。

我不確定我是否正確解決了您的問題,但是我猜您的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();
}

下面是我的主活動中調用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.
}

您將必須使用以下內容覆蓋MainActivity中onActivityResult

@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官方文檔中的此鏈接 ,那里有很多有用的東西。

我希望這可以幫助你。

在Receiver Activity中從意圖中獲取數據,如下所示

Intent intent = getIntent();
string text = intent.getStringExtra("t");

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM