2015-01-17 80 views
-1

我試圖檢索一些已添加到套件中的數據,但我無法使用正常方式檢索它。無法從套件中檢索數據

我這工作當前的代碼是

Intent i = getIntent(); 
Bundle bundle = i.getExtras();  
for (String key : bundle.keySet()) { 
        Object value = "start_color"; 
        start_color = String.format("%s", key);     
       } 

這讓我捆價值,但是當我使用一個標準的代碼是這樣

Intent i = getIntent(); 
Bundle bundle = i.getExtras(); 
    if (bundle != null) 
    { 
    String value = bundle.getString("start_color"); 
    } 

是無法獲得的束值我很困惑爲什麼發生這種情況!有人能告訴我爲什麼會發生這種情況嗎?

這就是我把價值成束

String rgbString = "R: " + Color.red(color) + " B: " + Color.blue(color) + " G: " + Color.green(color); 

       //Change activity send data 
       Intent device = new Intent(v.getContext(), Create_Preset.class); 
       device.putExtra(rgbString,"start_color"); 
       startActivity(device); 
       overridePendingTransition(R.anim.left_in, R.anim.left_out); 

我甚至通過REST API,因此那些想知道,如果包有任何增值新行插入我的使用改變代碼預設表的代碼,是的,它確實!!

這裏是我的存儲值看到對數據庫的選擇start_colorend_color這些都是我從包獲取值

Host: localhost 
Database: led 
Generation Time: Jan 17, 2015 at 04:37 PM 
Generated by: phpMyAdmin 4.2.7.1/MySQL 5.6.20 
SQL query: SELECT * FROM `preset` WHERE 1 LIMIT 0, 25 ; 
Rows: 2 
Current selection does not contain a unique column. Grid edit, checkbox, Edit, Copy and Delete features are not available. 
preset starttime_hour starttime_min endtime_hour endtime_min  startColor endColor 
test 12 0 12 0 R: 159 B: 159 G: 159 R: 159 B: 159 G: 159 
test2  12 0 12 0 R: 39 B: 122 G: 91 R: 39 B: 122 G: 91 
+0

您確定該包含有該值嗎?在代碼中顯示你把值放在包中的代碼 – Marcus

+0

@Marcus包中包含值我怎麼才能得到它與改變的代碼? –

+0

您正在反轉鍵和值。 – corsair992

回答

1

您正在添加值作爲鍵和鍵作爲值。

正確: Intent.putExtra(鍵,值)

你有什麼: device.putExtra(rgbString, 「start_color」);

-1

您需要使用Iterator接口來從密鑰集中獲取值作爲bundle.keySet返回一組字符串。 例如對於

Set<String> keys = bundle.keySet(); 
Iterator<String> itr = keys.iterator(); 
while(itr.hasNext()) { 
       String key = itr.next(); 
       String value = bundle.get(key); 
} 
+0

這是不正確的和不相關的。 foreach循環適用於所有'Iterable'實現。 – corsair992