2014-12-04 57 views
0

傢伙正在基於JSON格式動態添加的最後一行

{"Seat Availability":[{"Ship":"Kavaratti","Departure Date":"10/12/2014"},{"Class":"Bunk","Seats":"178"},{"Class":"Bunk","Seats":"200"},{"Class":"Bunk","Seats":"214"},{"Class":"Bunk","Seats":"246"},{"Class":"First","Seats":"0"},{"Class":"Second","Seats":"112"},{"Class":"Second","Seats":"120"},{"Class":"Second","Seats":"95"},{"Class":"Second","Seats":"97"},{"Ship":"Kavaratti","Departure Date":"22/12/2014"},{"Class":"Bunk","Seats":"0"},{"Class":"Bunk","Seats":"1"},{"Class":"First","Seats":"0"},{"Class":"Second","Seats":"10"},{"Class":"Second","Seats":"19"},{"Class":"Second","Seats":"24"},{"Class":"Second","Seats":"6"},{"Ship":"Kavaratti","Departure Date":"03/01/2015"},{"Class":"Bunk","Seats":"229"},{"Class":"Bunk","Seats":"256"},{"Class":"First","Seats":"0"},{"Class":"Second","Seats":"105"},{"Class":"Second","Seats":"109"}]} 

一切工作,除了最後一艘船的詳細信息創建表。我不明白爲什麼最後一艘船的細節沒有顯示。

下面是解析的代碼,我用

 TableLayout ll = (TableLayout) findViewById(R.id.tableLayoutProduct); 


     JSONObject json = null; 
     try { 
      json = new JSONObject(result); 
     } catch (JSONException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
     result = ""; 
     JSONArray arr = null; 
     try { 
      arr = json.getJSONArray("Seat Availability"); 
     } catch (JSONException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
     TextView heading = (TextView) findViewById(R.id.heading); 
     heading.setText("Seat Availability: "+destination); 


     for(int i=0,j=0,z=0; i<arr.length(); i++){  
      TableRow tbrow1 = new TableRow(HttpExample.this); 
      TableRow tbrow2 = new TableRow(HttpExample.this); 
      TableRow tbrow3 = new TableRow(HttpExample.this); 

      tbrow1.setBackgroundColor(Color.parseColor("#C9D1F7")); 
      if(i==0){ 
      TextView tv4 = new TextView(HttpExample.this); 
      tv4.setText(" Ship "); 
      TextView tv5 = new TextView(HttpExample.this); 
      tv5.setText(" Departure Date ");    
      tbrow1.addView(tv4); 
      tbrow1.addView(tv5); 
      } 
      else{ 
       TextView tv3 = new TextView(HttpExample.this); 
       tv3.setText(" "); 
       TextView tv4 = new TextView(HttpExample.this); 
       tv4.setText(" ");     
       tbrow1.addView(tv3); 
       tbrow1.addView(tv4); 

      } 

      TextView tv1=new TextView(HttpExample.this); 
      TextView departure_date = new TextView(HttpExample.this); 
      TextView tv2 = new TextView(HttpExample.this); 
      TextView index = new TextView(HttpExample.this); 

      try { 
       jo = arr.getJSONObject(i); 
       result = jo.get("Ship").toString(); 
       j++; 
       tv1.setText(result+" "); 
       tv1.setEms(5); 
       tbrow2.addView(tv1);      
       result = (String) jo.get("Departure Date"); 
       departure_date.setEms(7); 
       departure_date.setText(result+" "); 
       tbrow2.addView(departure_date); 
       i++; 
       jo = arr.getJSONObject(i); 
       TextView[] tv_heading = new TextView[9]; 
       TextView[] tv_value = new TextView[9];        
       while(jo.has("Class")) 
        { 
        tv_heading[z] = new TextView(HttpExample.this); 
        tv_value[z] = new TextView(HttpExample.this); 
        tv_heading[z].setEms(4); 
        tv_value[z].setEms(4); 
        tv_heading[z].setText(jo.get("Class").toString()+" "); 
        seats = Integer.parseInt(jo.getString("Seats").toString()); 
        if(seats==0) 
         tv_value[z].setTextColor(Color.RED); 
        else 
         tv_value[z].setTextColor(Color.parseColor("#160A98")); 
        tv_value[z].setText(jo.get("Seats").toString()); 
        tbrow1.addView(tv_heading[z]); 
        tbrow2.addView(tv_value[z]);       
        i++; 
        z++; 
        jo = arr.getJSONObject(i);  

        } 
       i--; 
       z=0; 
       TextView blank = new TextView(HttpExample.this); 
       blank.setText(" "); 
       tbrow3.addView(blank); 
       ll.addView(tbrow1); 
       ll.addView(tbrow2); 
       ll.addView(tbrow3); 
      } catch (JSONException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 

請任何一個能指出問題

+0

'for(int i = 0,j = 0,z = 0; i <= arr.length(); i ++)'試試這個 – 2014-12-04 07:13:41

+0

ya試過了..雖然沒有工作 – 2014-12-04 07:28:51

+0

發現了問題..在while循環中需要一個break語句if(i> = arr.length())break; – 2014-12-04 10:52:47

回答

0

發現了問題。它需要在while循環 break語句我++ 如果後(ⅰ> = arr.length())斷裂;

相關問題