我目前正在嘗試通過REST API調用獲取數據,解析它以獲取我需要的信息,然後將該信息傳遞給新活動。我使用來自loopj.com的異步HTTP客戶端作爲REST客戶端,然後分別將onClick
和onCreate
的代碼用於當前和未來的活動。使用包將數據從一個活動傳遞到另一個活動 - 不在第二個活動中顯示
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);
}
}
感謝您的幫助。非常感謝。
這是一個非常簡潔但非常詳細的答案。感謝你寫得這麼清楚。它幫助我瞭解如何快速使用捆綁軟件。 – raddevus 2016-02-28 15:48:34
謝謝。這個例子幫助了我。 – Edwinfad 2016-11-26 18:21:10