2015-07-03 163 views
5

我讀文件約getInt()方法:android.os.Bundle中getInt(string key)返回的整數是什麼?

公衆詮釋getInt(String鍵)

返回與給定鍵相關聯的值,或0,如果沒有的 映射所需的類型存在用於給定的關鍵。

參數:

鑰匙串

回報:

int值

,但我不能得到它什麼它究竟回報。

key的ID是在R.java還是沒有別的?

+2

密鑰的值例如{「data」:1},getInt(「data」)= 1 – Proxytype

+1

它返回的內容取決於您發送的參數(如說明所解釋的)。 如果你有一組{{「one」,1},{「two」,2}}「one」將返回1,「two」將返回2,「three」將返回0 – Stultuske

+0

@Proxytype參數是一個字符串,那麼{key =「possition」; } ??? –

回答

4

它使用相同的密鑰返回您在該捆綁中放置的任何內容。

Bundle bundle = new Bundle(); 
bundle.putInt("KEY", 1); 
int value = bundle.getInt("KEY"); // returns 1 

它只是一個map/dictionary數據類型,其中您將字符串值與其他值進行映射。如果您有其他數據類型,則應該爲該數據類型使用適當的put/get-methods。

3

沒有什麼更好的是用一個例子

假設你有兩個活動:活動1和活性2和要傳遞beetwen則數據:

活性1

private static final String MY_KEY = "My Key" 
Intent intent = new Intent(Activity1.this, Activity2.class); 
Bundle b = new Bundle(); 

b.putInt(MY_KEY, 112233); 

intent.putExtras(b); 

startActivity(intent); 

活動2

private static final String MY_KEY = "My Key" 
Bundle b = getIntent().getExtras(); 

int value = b.getInt(MY_KEY , 0); 

//value now have the value 112233 

什麼意思是「返回與給定鍵關聯的值,如果給定鍵不存在所需類型的映射,則返回0。 MY_KEY「在這個例子嗎?

使用捆綁你從活動1使用鍵發送值到活動2‘’所以,‘MY_KEY’與112233.

正如你有關可以看到有第二個參數「0」。

這是默認值。在情況下,當包不包含 數據,您將收到「0」(默認值)。

相關問題