2013-03-16 22 views
12

我目前正在嘗試通過REST API調用獲取數據,解析它以獲取我需要的信息,然後將該信息傳遞給新活動。我使用來自loopj.com的異步HTTP客戶端作爲REST客戶端,然後分別將onClickonCreate的代碼用於當前和未來的活動。使用包將數據從一個活動傳遞到另一個活動 - 不在第二個活動中顯示

Eclipse沒有爲我的任何代碼傳遞任何錯誤,但是當我嘗試在模擬器中運行時,當新的活動/視圖打開時,我什麼都沒有(即空白的白色屏幕)。我試圖在我的REST客戶端中使用不同的URL進行編碼,但我仍然看不到任何內容。我甚至通過將onClick中的try/catch和venueName中的bundle.putString("VENUE_NAME", venueName);更改爲searchTerm,將API調用從等式中刪除。儘管如此,新的觀點出現,但沒有顯示。什麼沒有通過,或者我忘了做什麼第二項活動顯示venueName?在第二活動

public void onClick(View view) { 
    Intent i = new Intent(this, ResultsView.class); 
    EditText editText = (EditText) findViewById(R.id.edit_message); 
    String searchTerm = editText.getText().toString(); 


    //call the getFactualResults method 
    try { 
     getFactualResults(searchTerm); 
    } catch (JSONException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 

    //Create the bundle 
    Bundle bundle = new Bundle(); 
    //Add your data from getFactualResults method to bundle 
    bundle.putString("VENUE_NAME", venueName); 
    //Add the bundle to the intent 
    i.putExtras(bundle); 

    //Fire the second activity 
    startActivity(i); 
} 

方法應該接收的意圖和捆綁並顯示它:

protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    //Get the message from the intent 
    //Intent intent = getIntent(); 
    //String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE); 

    //Get the bundle 
    Bundle bundle = getIntent().getExtras(); 

    //Extract the data… 
    String venName = bundle.getString(MainActivity.VENUE_NAME);   

    //Create the text view 
    TextView textView = new TextView(this); 
    textView.setTextSize(40); 
    textView.setText(venName); 

    //set the text view as the activity layout 
    setContentView(textView); 

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) { 
     getActionBar().setDisplayHomeAsUpEnabled(true); 
    } 
} 

感謝您的幫助。非常感謝。

回答

10

兩種方法可以發送數據。這是你現在如何發送它。它沒有任何問題。

//Create the bundle 
Bundle bundle = new Bundle(); 
//Add your data from getFactualResults method to bundle 
bundle.putString("VENUE_NAME", venueName); 
//Add the bundle to the intent 
i.putExtras(bundle); 
startActivity(i); 

在你的代碼(第二活動),但是,你指的是key在捆綁爲MainActivity.VENUE_NAME但沒有在代碼中暗示,你必須返回作爲實際key名稱與發送值一類束。在第二活動你的代碼改成這樣:

Bundle bundle = getIntent().getExtras(); 

//Extract the data… 
String venName = bundle.getString("VENUE_NAME");   

//Create the text view 
TextView textView = new TextView(this); 
textView.setTextSize(40); 
textView.setText(venName); 

你可以在你的第二個活動,檢查包中包含使用這個關鍵,你會知道,key是不存在的包。然而,上面的更正會讓它爲你工作。

if (bundle.containsKey(MainActivity.VENUE_NAME)) { 
    .... 
} 
+1

這是一個非常簡潔但非常詳細的答案。感謝你寫得這麼清楚。它幫助我瞭解如何快速使用捆綁軟件。 – raddevus 2016-02-28 15:48:34

+0

謝謝。這個例子幫助了我。 – Edwinfad 2016-11-26 18:21:10

0

確保您用作將項目放入捆綁包的密鑰的字符串與用於提取它的密鑰相同。在你的情況,也許MainActivity.VENUE_NAME是不一樣的「VENUE_NAME」

1

發送捆綁。

Bundle bundle = new Bundle(); 
bundle.putString("Name",Object); //This is for a String 
i.setClass(CurrentClass.this, Class.class); 
i.putExtras(bundle); 
startActivity(i); 

接收束

Bundle bundle = null; 
bundle = this.getIntent().getExtras(); 
String myString = bundle.getString("Name");//this is for String 
+0

這裏是什麼是「我」。請解釋我的意思 – 2017-05-25 08:27:47

+0

如果您定義意圖i = new Intent();我已經試過這個請解釋你的答案 – 2017-05-25 08:28:50

2

您錯誤地訪問了您在捆綁中添加的密鑰。除了獲取字符串作爲MainActivity.VENUE_NAME嘗試直接通過你的包,如下所添加的鍵名:

除了如下獲取字符串:

//Get the bundle 
    Bundle bundle = getIntent().getExtras(); 
    //Extract the data… 
    String venName = bundle.getString(MainActivity.VENUE_NAME);   

嘗試使用密鑰來獲得字符串命名如下:

/Get the bundle 
    Bundle bundle = getIntent().getExtras(); 
    //Extract the data… 
    String venName = bundle.getString("VENUE_NAME"); 
4

我想如果你有

String venName = bundle.getString("VENUE_NAME"); 

更換

String venName = bundle.getString(MainActivity.VENUE_NAME); 

它應該工作。

這是我如何處理我的數據傳輸從一個活動到另一個:

將數據發送到活動稱爲 「Projectviewoptions」:

Bundle b = new Bundle(); 
      b.putString("id", str_projectid); 
      Projectviewoptions pv = new Projectviewoptions(); 

接收數據:

idbundle = getArguments(); 
String myid = idbundle.getString("id"); 

的雙方的「關鍵」應該相同;在這種情況下是「id」。

另一種方式來傳送數據,通過意圖是:

發送:

Intent intent = new Intent(getActivity(),ViewProjectDetails.class); 
          intent.putExtra("id", myid); 
          startActivity(intent); 

收到:

String id = getIntent().getExtras().getString("id"); 
0

意向loginIntent =新意圖(LoginActivity.this,HomeActivity。類);

Bundle bundle = new Bundle();

bundle.putString(「user_id」,userId);

i.putExtras(bundle);

startActivity(loginIntent);

LoginActivity.this.finish();

相關問題