2012-06-12 20 views
2

我想通過從JSON字符串中提取的Int值來減少代碼冗餘。如何獲取JSON字符串到Int然後到Arg Bundle?

在我的JSON文件中,我在「resFile」中有一個字符串值。我將這個字符串存儲到TAG_RES_FILE我想在Bundle中將它作爲Int傳遞。

如果你看我的代碼,你會看到評論// TRY#1 //。這按預期工作,但我需要Int來自一個變量,它存儲我的TAG_RES_FILE。在評論// TRY#2 //僅僅是我想要的功能的一個例子 - 顯然它沒有。在下一行,我試過標籤字符串轉換爲詮釋但這給出了一個運行時錯誤:

java.lang.NumberFormatException:無效INT:「resFile」

我有甚至嘗試將0x7f060000(來自R.java)放入JSON字符串中。

所以我的問題是:我該如何做到這一點?我是走在正確的軌道上還是應該以完全不同的方式去探索?

Thnx爲您的幫助和輸入 - 請在您的答案中顯示代碼示例。

JSON字符串SNIPPIT:

[ 
    { 
     "_id": "1", 
     "label": "A Lable", 
     "title": "Some Title", 
     "description": "Bla, bla, bla", 
     "containerID": "Some container id", 
     "isRawRes": "boolean value here", 
     "resFile": "R.raw.advisory_circulators_sort_list" 
    }, {. . . 
] 

在我的HashMap:

// Parse the string to a JSON object 
     for (int i = 0; i < json.length(); i++) { 
      JSONObject json_data = json.getJSONObject(i); 

      // Storing each json item in variable 
      String id = json_data.getString(TAG_ID); 
      String label = json_data.getString(TAG_LABEL); 
      String title = json_data.getString(TAG_TITLE); 
      String description = json_data.getString(TAG_DISCR); 
      String containerID = json_data.getString(TAG_FRAG_ID); 
      String isRawRes = json_data.getString(TAG_IS_RAW_RES); 
      String resFile = json_data.getString(TAG_RES_FILE); 

      // creating new HashMap 
      HashMap<String, String> map = new HashMap<String, String>(); 

      // adding each child node to HashMap key => value 
      map.put(TAG_ID, id); 
      map.put(TAG_LABEL, label); 
      map.put(TAG_TITLE, title); 
      map.put(TAG_DISCR, description); 
      map.put(TAG_FRAG_ID, containerID); 
      map.put(TAG_IS_RAW_RES, isRawRes); 
      map.put(TAG_RES_FILE, resFile); 

      // adding HashList to ArrayList 
      mList.add(map); 
} 

在我的列表視圖setOnItemClickListener:

public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
    . . . 

     final Bundle args = new Bundle(); 

//TRY #1//int rawRes = R.raw.advisory_circulators_sort_list; <--I NEED TO GET THIS IN FROM MY TAG!! 
//TRY #2//int rawRes = TAG_RES_FILE; <-- TO SOMETHING LIKE THIS!! 
     int passResFile = Integer.parseInt(TAG_RES_FILE);//<--THIS GIVES A NPE!! 
     args.putInt("KEY_RES_FILE", passResFile); 

     bolean isRawRes = true; 
     args.putBoolean("KEY_IS_RAW_RES", isRawRes); 

     // Delayed to improve animations 
     final Handler handler = new Handler(); 
     handler.postDelayed(new Runnable() { 
      public void run() { 
       ListViewFragment lvf = new ListViewFragment(); 
       lcFT.replace(R.id.listContainer, lvf).commit(); 
       lvf.setArguments(args); 
      } 
     }, 300); 
    } 

回答

1

相反,只存儲advisory_circulators_sort_list而不是R.raw.advisory_circulators_sort_list。然後,要獲得整數標識符,使用此方法:

int passResFile = getResources().getIdentifier(resFile, "raw", getPackageName()); 
+0

Thanx a ton !!!! – CelticParser

+0

你可以看看[在這裏](http://stackoverflow.com/q/11003846/742030)?它與這個Q. Thnx有關 – CelticParser