2013-08-01 27 views
0

我想顯示列i中的json數據m顯示的數據與此圖像相同http://imgur.com/v4WpJdN我遵循本教程顯示json在tablelayout http://kahdev.wordpress.com/2012/02/19/android-parsing-json-with-jsonobject/ 但我的josn不會顯示。修改我的JSON文件中創建新的數組「接觸」,但它不會顯示在屏幕上,請查看我的JSON解析代碼解析「聯繫」」如何在列中顯示json?我的json不顯示

 public class fifthscreen extends Activity{ 



HorizontalListView listview; 
CategoryListAdapter3 cla; 
String DescriptionAPI; 

static ArrayList<Long> Category_ID = new ArrayList<Long>(); 
static ArrayList<String> Category_name = new ArrayList<String>(); 
static ArrayList<String> Category_image = new ArrayList<String>(); 


@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.fifthscreen); 


    listview = (HorizontalListView) this.findViewById(R.id.listview2); 

     cla = new CategoryListAdapter3(fifthscreen.this); 


     DescriptionAPI = Utils.DescriptionAPI; 

     //  clearData(); 
      try { 

       HttpClient client = new DefaultHttpClient(); 

    HttpConnectionParams.setConnectionTimeout(client.getParams(), 15000); 
       HttpConnectionParams.setSoTimeout(client.getParams(), 
     15000); 
       HttpUriRequest request = new HttpGet(DescriptionAPI); 
       HttpResponse response = client.execute(request); 
       InputStream atomInputStream = 
    response.getEntity().getContent(); 
       BufferedReader in = new BufferedReader(new 
      InputStreamReader(atomInputStream)); 

       String line; 
       String str = ""; 
       while ((line = in.readLine()) != null){ 
        str += line; 
       } 


        JSONObject json = new JSONObject(str); 
        JSONArray data = 
      json.getJSONArray("worldpopulation"); 


        for (int i = 0; i < data.length(); i++) { 
         JSONObject object = data.getJSONObject(i); 

        // JSONObject category = 
     object.getJSONObject("Category"); 


         Category_ID.add(Long.parseLong(object.getString("rank"))); 
         Category_name.add(object.getString("name")); 
         Category_image.add(object.getString("url")); 

         Log.d("Category name", Category_name.get(i)); 
         listview.setAdapter(cla); 




     ////Acess Json array Contacts///////////////////// 


         JSONObject json2 = new JSONObject(str); 
         JSONArray data2 = 
     json.getJSONArray("contacts"); 
        final TableLayout table = (TableLayout) 
        findViewById(R.id.table); 
        for (int j = 0; i < data.length(); j++) { 
         final View row = 
      createRow(data.getJSONObject(i)); 
         table.addView(row); 


        } 










        } 


      } catch (MalformedURLException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } catch (IOException e) { 
       // TODO Auto-generated catch block 
      // IOConnect = 1; 
       e.printStackTrace(); 
      } catch (JSONException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 




    } 


public View createRow(JSONObject item) throws JSONException { 
     View row = getLayoutInflater().inflate(R.layout.rows, null); 
     ((TextView) row.findViewById(R.id.localTime)).setText(item 
       .getString("Item")); 
     ((TextView) row.findViewById(R.id.apprentTemp)).setText(item 
       .getString("Quantity")); 

     return row; 
    } 
    } 


    //////my son file i add new array "contact"/////////// 
     { 
"worldpopulation": [ 
    { 
"rank":1, 
"name": "Angelina", 
"url": "http://www.bounty4u.com/android/images/angie.jpg" 
}, 
{ 
"rank":2, 
"name": "Ashton ", 
"url": "http://www.bounty4u.com/android/images/ashton.jpg" 
}, 
{ 
"rank":3, 
"name": "Jackman", 
"url": "http://www.bounty4u.com/android/images/hugh.jpg" 
} 

] 
, 

"contacts": [ 
      { 
    "id":1, 
"Item": "Calories", 
"Quantity": "150g" 
}, 
{ 
    "id":2, 
"Item": "Calcium", 
"Quantity": "250g" 
}, 
{ 
"id":3, 
"Item": "Carbohydrates", 
"Quantity": "300g" 
} 

] 
} 







    public class CategoryListAdapter3 extends BaseAdapter { 

private Activity activity; 
private ImageLoader imageLoader; 

public CategoryListAdapter3(Activity act) { 
    this.activity = act; 
    imageLoader = new ImageLoader(act); 
} 

public int getCount() { 
    // TODO Auto-generated method stub 
    return fifthscreen.Category_ID.size(); 
} 

public Object getItem(int position) { 
    // TODO Auto-generated method stub 
    return position; 
} 

public long getItemId(int position) { 
    // TODO Auto-generated method stub 
    return position; 
} 

public View getView(int position, View convertView, ViewGroup parent) { 
    // TODO Auto-generated method stub 
    ViewHolder holder; 

    if(convertView == null){ 
     LayoutInflater inflater = (LayoutInflater) activity 
       .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     convertView = inflater.inflate(R.layout.viewitem2, null); 
     holder = new ViewHolder(); 

     convertView.setTag(holder); 
    }else{ 
     holder = (ViewHolder) convertView.getTag(); 
    } 


    holder.txtText = (TextView) convertView.findViewById(R.id.title2); 
    holder.imgThumb = (ImageView) convertView.findViewById(R.id.image2); 

    holder.txtText.setText(fifthscreen.Category_name.get(position)); 
// 
imageLoader.DisplayImage(Utils.AdminPageURL+CategoryList.Category_image.get(position), 
     imageLoader.DisplayImage(fifthscreen.Category_image.get(position), 
      activity, holder.imgThumb); 

    return convertView; 
} 


} 
+0

我只是廣告新陣「聯繫人」在我的JSON文件,並與本教程的幫助解析http://kahdev.wordpress.com/2012/02/19/android-parsing-json-with-jsonobject/ – user2589245

+0

這是完整的示例代碼https://github.com/kahgoh/Android-Parsing-JSON-with- JSONObject/blob/master/src/kah/json/JsonSampleActivity.java – user2589245

+0

只需查看下面這行代碼//// Acess Json array聯繫方式//////////////// /////用於訪問聯繫人數組 – user2589245

回答

0

請把命令行下面的代碼。

//object.getJSONObject("Category"); 

(因爲沒有JSONObject的名字「類別」)

現在這將是工作的順利進行。

+0

您是否意味着評論該行? – Harry

+0

@哈利:是的,必須註釋掉。 – saravanakumar