简体   繁体   English

我想以动态方式以表格形式制作这个json,我如何在android中制作它?

[英]I want to make this json in table form in dynamic way, how can i make it in android?

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\"}]}";

Here is My Code :这是我的代码:

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

create a listview and add values to it.创建一个列表视图并向其添加值。

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

Get your json values and add it in 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;
    }

}

Note: These border style is for draw your border.xml in table form like box type .注意:这些边框样式用于以表格形式绘制您的 border.xml,如 box type 。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM