如何显示在列JSON?我的JSON不显示 [英] how to show json in column?? my json not display

查看:126
本文介绍了如何显示在列JSON?我的JSON不显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要显示在列IM JSON数据显示,同样喜欢这一形象 http://imgur.com/v4WpJdN我按照这个教程,显示tablelayout的 HTTP://kahdev.word$p$pss.com/2012/02/19/android-parsing-json-with-jsonobject/
但我的josn将不显示。编辑我的JSON文件创建新的数组联系,但它不会显示在屏幕上,请查看我的JSON解析code的解析​​联系

 公共类fifthscreen延伸活动{Horizo​​ntalListView列表视图;
CategoryListAdapter3 CLA;
串DescriptionAPI;静态的ArrayList<龙> CATEGORY_ID =新的ArrayList<龙>();
静态的ArrayList<串GT; CATEGORY_NAME =新的ArrayList<串GT;();
静态的ArrayList<串GT; Category_image =新的ArrayList<串GT;();
@覆盖
公共无效的onCreate(捆绑savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.fifthscreen);
    列表视图=(Horizo​​ntalListView)this.findViewById(R.id.listview2);      CLA =新CategoryListAdapter3(fifthscreen.this);
      DescriptionAPI = Utils.DescriptionAPI;         // clearData();
            尝试{                HttpClient的客户端=新DefaultHttpClient();  HttpConnectionParams.setConnectionTimeout(client.getParams(),15000);
                HttpConnectionParams.setSoTimeout(client.getParams(),
         15000);
                HttpUriRequest要求=新HTTPGET(DescriptionAPI);
                HTT presponse响应= client.execute(请求);
                InputStream的atomInputStream =
     。response.getEntity()的getContent();
                在的BufferedReader =新的BufferedReader(新
          InputStreamReader的(atomInputStream));                串线;
                字符串str =;
                而((行= in.readLine())!= NULL){
                    STR + =行;
                }
                    JSONObject的JSON =新的JSONObject(STR);
                    JSONArray数据=
           json.getJSONArray(世界人口);
                    的for(int i = 0; I< data.length();我++){
                        的JSONObject对象= data.getJSONObject(I)                    //类的JSONObject =
        object.getJSONObject(类别);
                        Category_ID.add(的Long.parseLong(object.getString(等级)));
                        Category_name.add(object.getString(名字));
                        Category_image.add(object.getString(URL));                        Log.d(类别名称,Category_name.get(I));
                        listview.setAdapter(CLA);
      ////接取JSON数组联系人/////////////////////
                        JSONObject的json2 =新的JSONObject(STR);
                        JSONArray数据2 =
         json.getJSONArray(联系人);
                    最后TableLayout表=(TableLayout)
                    findViewById(R.id.table);
                    对于(INT J = 0; I< data.length(); J ++){
                        最终视图行=
             createRow(data.getJSONObject(I));
                        table.addView(行);
                    }
                    }
            }赶上(MalformedURLException的E){
                // TODO自动生成catch块
                e.printStackTrace();
            }赶上(IOException异常五){
                // TODO自动生成catch块
            // IOConnect = 1;
                e.printStackTrace();
            }赶上(JSONException E){
                // TODO自动生成catch块
                e.printStackTrace();
            }
  }
公共查看createRow(JSONObject的项目)抛出JSONException {
        查看排= getLayoutInflater()膨胀(R.layout.rows,NULL);
        ((的TextView)row.findViewById(R.id.localTime))。的setText(项目
                .getString(项目));
        ((的TextView)row.findViewById(R.id.ap prentTemp))的setText。(项目
                .getString(数量));        返回行;
    }
   }
   //////我的儿子我的文件添加新阵联系///////////
       {
 世界人口: [
  {
等级:1,
名:安吉丽娜
 URL:http://www.bounty4u.com/android/images/angie.jpg
 },
 {
等级:2,
名:阿什顿,
 URL:http://www.bounty4u.com/android/images/ashton.jpg
},
{
等级:3,
名:杰克曼
 URL:http://www.bounty4u.com/android/images/hugh.jpg
 }]
, 人脉:[
             {
  标识:1,
项目:卡路里,
 量:150克
 },
 {
  ID:2,
 项目:钙,
 量:250克
 },
 {
ID:3,
项目:碳水化合物
 量:300克
 }]
}  公共类CategoryListAdapter3延伸BaseAdapter {私人活动活动;
私人ImageLoader的ImageLoader的;公共CategoryListAdapter3(活动行为){
    this.activity =行为;
    ImageLoader的=新ImageLoader的(行为);
}公众诠释的getCount(){
    // TODO自动生成方法存根
    返回fifthscreen.Category_ID.size();
}公共对象的getItem(INT位置){
    // TODO自动生成方法存根
    返回的位置;
}众长getItemId(INT位置){
    // TODO自动生成方法存根
    返回的位置;
}公共查看getView(INT位置,查看convertView,父母的ViewGroup){
    // TODO自动生成方法存根
    ViewHolder持有人;    如果(convertView == NULL){
        LayoutInflater吹气=(LayoutInflater)活动
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = inflater.inflate(R.layout.viewitem2,NULL);
        持有人=新ViewHolder();        convertView.setTag(保持器);
    }其他{
        支架=(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(位置));
//
 imageLoader.DisplayImage(Utils.AdminPageURL + CategoryList.Category_image.get(位置),
        imageLoader.DisplayImage(fifthscreen.Category_image.get(位置),
            活动中,holder.imgThumb);    返回convertView;
}
 }


解决方案

请把命令行下面code。

  // object.getJSONObject(类别);

(因为没有的JSONObject按姓名类别)

现在这将是工作的顺利进行。

i want to show json data in column i m showing data same like this image http://imgur.com/v4WpJdN i follow this tutorial which display json in tablelayout http://kahdev.wordpress.com/2012/02/19/android-parsing-json-with-jsonobject/ but my josn will not display. i edit my json file create new array"contact" but it will not display on screen please review my json parsing code for parse "Contact""

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


 }

解决方案

Please put the command line to the following code.

//object.getJSONObject("Category");

(because there is no JSONObject by name "Category")

now it will be work smoothly.

这篇关于如何显示在列JSON?我的JSON不显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆