2013-08-01 34 views
1

如何在桌面佈局中顯示此內容?我想顯示這樣的json文件: Image1如何在TableLayout中顯示JSON?

在我的代碼下面的網絡連接工作,它解析圖像名稱和URL並顯示此。我只想知道如何在項目數量等json文件中添加一些新屬性。

和訪問我的json解析代碼並顯示在像這個圖像相同的佈局。

這是我的JSON文件

{ "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" 
     } 
    ] 
} 

這是我的代碼:

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); 


        } 


      } 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 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

只是廣告新陣列的鏈接怎麼辦IDO那??? – user2589245

回答

0

你爲什麼要使用表格佈局。相同的功能可以作爲一個列表視圖,它很容易使用,所以我建議你使用這個功能。它提供了多個列設施作爲tablelayout。

請參閱本link

+0

我不知道如何顯示這樣的json數據。請在我的json文件中添加一些虛擬「項目」「數量」,就像這張圖片http://imgur.com/v4WpJdN,並在屏幕上顯示,就像這個屏幕一樣,請幫助我 – user2589245

+0

即時消息不使用字符串我得到數據形式的json文件。做我litle贊成添加一些節點或數組在mymy json文件中,並顯示它們像這樣http://imgur.com/v4WpJdN plz – user2589245

+0

我不知道這是顯示在圖像http://imgur.com/v4WpJdN數組或json文件中的多個節點我粘貼我上面的json文件只需添加一些新的數據,如「項目」數量「訪問y的json代碼和顯示相同的像這樣的屏幕http://imgur.com/v4WpJdN – user2589245

0

使用谷歌GSON

GSON是可以用來將Java對象轉換成JSON的表示形式的Java庫。它也可以用來將JSON字符串轉換爲等效的Java對象。 Gson可以使用任意Java對象,包括您沒有源代碼的預先存在的對象。

它很容易在PHP中使用json_encode()

看到我的JSON文件數量的項目,並在佈局訪問和顯示以下

read

+0

請回復我的這個發帖http://stackoverflow.com/questions/17990514/how-to-show-json-in-column-my-json-not-display – user2589245