有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java我想用动态的方式把这个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) 个答案

  1. # 1 楼答案

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

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

    获取json值并将其添加到ListContainer中

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

    //上下文、资源、对象

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

    注意:这些边框样式用于绘制边框。类似于box类型的表格形式的xml