2011-06-24 111 views
30

我有一段時間想通過Intent/Bundle s來確定什麼數據來到我的方法。我嘗試添加斷點來檢查數據,但我什麼也沒看到。也許是因爲它是Parcelable我不能在Eclipse中手動讀取它。如何知道捆綁包中提供了哪些數據?

例如,onActivityResult(int requestCode, int resultCode, Intent data)Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI)。我如何知道有哪些數據可用?注意,我沒有要求提供什麼數據,但是我怎麼知道這些數據,所以我可以將相同的想法應用到來自Android框架的任何Bundle/Intent?也許這很簡單,就像查看文檔一樣,但是我沒有看到數據的完整列表,並且我在Eclipse中看不到它。所以我迷失了方向。

回答

64

Bundle.keySet()讓你在包的所有鍵的列表。這就是說,通常你只是期望某些鍵和查詢它們,但是keySet()對於檢查你從某個地方得到的bundle是有用的。

+0

感謝。我如何知道問題是什麼?我現在看到關於它的文檔,是嗎? – user123321

+0

捆綁包實際上只是一個容器。它完全取決於將其打包的應用程序。如果是你自己的,那很明顯 - 無論你輸入什麼內容。如果是別人的(或來自操作系統),那麼它取決於應用程序放入的內容。選項1:檢查文檔。選項2:詢問應用程序的開發人員。選項3:使用Bundle.keySet()分析傳入的包。 – EboMike

+0

** WHERE **是用於startActivityForResult的ContactsContract.Contacts.CONTENT_URI包中的東西的文檔? – user123321

0

你擺脫捆綁的唯一的東西就是你放入的東西。捆綁是在活動之間傳遞信息的方式。如果你負責你的整個應用程序,你不需要在Bundle中查看你的對象,你應該抓住它們。認爲hashmap鍵...如果你不知道密鑰,它不像你可以搜索hashmap。

要將某個物品放入捆綁包並將其傳遞給下一個活動,您需要將其作爲Extra。查看here,瞭解通過活動之間的附加組件和捆綁來傳遞數據的示例。

複製和粘貼如下:

從活動1

Intent intent = new Intent(this,myActivity2.class); 
Bundle bundle = new Bundle(); 
bundle.putString(「myValue「, myValue); 
intent.putExtras(bundle); 
navigation.this.startActivity(intent); 

在活性2

Bundle bundle = getIntent().getExtras(); 
act2MyValue= bundle.getString(「myValue「); 
39
public static String bundle2string(Bundle bundle) { 
    if (bundle == null) { 
     return null; 
    } 
    String string = "Bundle{"; 
    for (String key : bundle.keySet()) { 
     string += " " + key + " => " + bundle.get(key) + ";"; 
    } 
    string += " }Bundle"; 
    return string; 
} 
+5

像你這樣的人是我爲什麼如此活躍的原因給我們現成的咖啡:P謝謝 – khandelwaldeval

2

我越來越alll鍵和存儲捆綁的價值...

for (String key : bundle.keySet()) { 
    string += " " + key + " => " + bundle.get(key) + ";"; 
} 

輸出:

(key)  :(value)  
profile_name:abc 
相關問題