2012-10-08 41 views
0

我試圖將數據存儲到包中並開始一個新的活動。然後在新的活動中,我想要取消數據。爲鑄造部分的代碼是follwing:通過意向傳遞一個包

Intent i = new Intent(MainActivity.this, ShowProductActivity.class); 
Bundle bundle = new Bundle(); 
// Getting adapter of the listview 
SimpleAdapter adapter = (SimpleAdapter) mListView.getAdapter(); 
bundle.putSerializable("param1", ((HashMap<String, Object>) adapter.getItem(position))); 
i.putExtras(bundle); 
startActivity(i); 

一般來說,數據包含到包是如下:

Bundle[{param1={position=0, image=2130837504, flag=/data/data/com.example.app/cache/wpta_0.jpeg, price=Brand : Dash Price : 27}}] 

我怎樣才能檢索到上述數據爲第二活動?我正在使用下面的代碼塊,但我無法訪問屬性。

Bundle bundle = this.getIntent().getExtras(); 
HashMap<String, Object> param1 = (HashMap<String, Object>)bundle.getSerializable("param1"); 
+0

你是什麼意思,你不能訪問屬性?當你嘗試索引HashMap中的數據時會發生什麼? –

回答

2

我不相信你需要創建一個包通過一個串行化,簡單地說了Serializable直入意圖演員:

i.putExtras("param1", ((HashMap<String, Object>) adapter.getItem(position))); 

或者如果必須使用中間捆綁,請查看文檔Bundle#putExtras(Bundle bundle)

添加一組擴展數據的意圖。鍵必須包含一個包前綴,例如com.android.contacts應用程序將使用「com.android.contacts.ShowAll」之類的名稱。