2014-10-11 88 views
0

我需要填充来自JSON数组“视频”中标记“group”下的数据的列表视图。它下面的编码工作正常,如果我删除条件。请帮助我们。我的老板正在踢我的屁股,因此我的工作就绪。在此先感谢从填充列表中获取json服务的具体数据列表

公共类CountryJSONParser {

// Receives a JSONObject and returns a list 
public List<HashMap<String,Object>> parse(JSONObject jObject){  

    JSONArray jCountries = null; 
    try {  
     // Retrieves all the elements in the 'countries' array 
     jCountries = jObject.getJSONArray("video"); 
     } 
    catch (JSONException e) { 
     e.printStackTrace(); 
    } 

    // Invoking getCountries with the array of json object 
    // where each json object represent a country 
    return getCountries(jCountries); 
} 


private List<HashMap<String, Object>> getCountries(JSONArray jCountries){ 
    int countryCount = jCountries.length(); 
    List<HashMap<String, Object>> countryList = new ArrayList<HashMap<String,Object>>(); 
    HashMap<String, Object> country = null; 

    // Taking each country, parses and adds to list object 
    for(int i=0; i<countryCount;i++){ 
     try { 
      // Call getCountry with country JSON object to parse the country 
      country = getCountry((JSONObject)jCountries.get(i)); 
      countryList.add(country); 

     } catch (JSONException e) { 
      e.printStackTrace(); 
     } 
    } 

    return countryList; 
} 

// Parsing the Country JSON object 
private HashMap<String, Object> getCountry(JSONObject jCountry){ 

    HashMap<String, Object> country = new HashMap<String, Object>(); 
    String countryName = ""; 
    String flag=""; 
    String language = ""; 
    String capital = ""; 
    String currencyCode = ""; 
    String currencyName = "";  

    try { 
     countryName = jCountry.getString("Description"); 
     flag = jCountry.getString("thumbnailUrl"); 
     capital = jCountry.getString("title"); 
     language=jCountry.getString("group"); 
     Log.v("---","Country name: "+countryName+"Flag Url:"+flag+"Title"+capital); 
      if(language.equals("RokuTest-VideoGroup")){  
     country.put("country", countryName); 
     country.put("group", language); 
     country.put("flag", R.drawable.ic_launcher); 
     country.put("flag_path", flag); 
     country.put("details", capital); 
     Log.v("---","Country name: "+countryName+"Flag Url:"+flag+"Title"+capital); 
      } 

    } catch (JSONException e) {   
     e.printStackTrace(); 
    }  
    return country; 
} 

}

回答

0

罗希特..只要管理中的代码,如果和别的块。它应该像你说的那样工作,如果工作正常。

if(language.equals("RokuTest-VideoGroup")) {  
    // do somthing 
} 
else { 
    // do something 
} 
+0

没有任何工作。尝试了不同的东西。还是一样。 – 2014-10-12 09:11:35

+0

你是否在日志猫中得到任何错误?你能给你解析的JSON响应吗? – 2014-10-12 09:32:04

+0

检查语言的价值,它包含什么并打印日志。我建议为JSON解析创建单独的类。看起来,如果你使用块,条件是假的,值不能存储在国家哈希映射。 – 2014-10-12 10:02:06