我想以动态方式将这个json以表格形式制作,我怎样才能在android中制作它?

时间:2015-06-29 06:46:24

标签: java android json

String jsonData="{\"sem1\" :[{\"subname\":\"TOC\",\"subcode\":\"1009\",\"subcredit\":\"6\",\"subgrade\":\"AB\"},{\"subname\":\"DS\",\"subcode\":\"10090\",\"subcredit\":\"5\",\"subgrade\":\"BB\"},{\"subname\":\"TOC\",\"subcode\":\"1009\",\"subcredit\":\"6\",\"subgrade\":\"AB\"}],\"sem2\":[{\"subname\":\"AAS\",\"subcode\":\"111009\",\"subcredit\":\"6\",\"subgrade\":\"AB\"},{\"subname\":\"AE\",\"subcode\":\"103309\",\"subcredit\":\"6\",\"subgrade\":\"DD\"}]}";

这是我的代码:

 protected String doInBackground(String... params1) {

        try {
            jsonObject = new JSONObject(jsondata);
        } catch (JSONException e) {
            e.printStackTrace();
        }

        Iterator<String> iter = jsonObject.keys();
        while (iter.hasNext()) {
            String key = iter.next();

            try {
                JSONArray jsonArray = jsonObject.getJSONArray(key);
                int length = jsonArray.length();
                for (int i = 0; i < length; i++) {
                    JSONObject jsonObject1 = jsonArray.getJSONObject(i);
                    subname = jsonObject1.getString("subname");
                    subcode = jsonObject1.getString("subcode");
                    subcredit = jsonObject1.getString("subcredit");
                    subgrade = jsonObject1.getString("subgrade");

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

        }

        return null;
    }

1 个答案:

答案 0 :(得分:0)

创建一个列表视图并为其添加值。

 private List<YourListcontainer> listContainer = new ArrayList<YourListcontainer>();

获取您的json值并将其添加到yourListContainer中。

 MyListAdapter adapter = new MyListAdapter();
 Listview lv = (ListView) rootView.findViewById(R.id.lvid);
 lv.setAdapter(adapter); 

 private class MyListAdapter extends ArrayAdapter<YourListcontainer> {

    public MyListAdapter() {
        // calls the base class constructor.
        super(context, R.layout.list_adapter, listContainer);

// context,resources,object

    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View itemview = convertView;
        LayoutInflater listView = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        if (itemview == null) {
            itemview = listView.inflate(R.layout.list_adapter,
                    parent, false);
        }
        final YourListContainer item = listContainer.get(position);// returns the index of the cell
        TextView subject = (TextView) itemview
                .findViewById(R.id.txvsubject);
        subject.setText(item.getSubjectname());
        TextView code= (TextView) itemview
                .findViewById(R.id.txvcode);
        code.setText(item.getCode());
        TextView credit = (TextView) itemview
                .findViewById(R.id.txvcredit );
        credit .setText(item.getCredit ());
        TextView grade = (TextView) itemview
                .findViewById(R.id.txvgrade );
        grade .setText(item.getGrade ());


            subject.setBackgroundResource(R.drawable.borderstyle);
            code.setBackgroundResource(R.drawable.borderstyle);
            credit.setBackgroundResource(R.drawable.borderstyle);
            grade.setBackgroundResource(R.drawable.borderstyle);


        return itemview;
    }

}

注意:这些边框样式用于以表格形式绘制border.xml,如框类型。