使用Section将结果JSON Parse显示到Custom Listview

时间:2017-05-15 04:52:00

标签: android json listview

我已经成功在调试时解析我的API。所以,我想向listview显示一些数据,但我的listview仍默认使用硬编码:

    ArrayList<Object> list = new ArrayList<>();
    list.add(new String("Persiapanasas Lahan"));
    list.add(new timelinedetail_modul(1 ,"Informasi Tantangan dan Kelengkapan Data", R.drawable.statuscolor));
    list.add(new timelinedetail_modul(2,"Artikel Manajemen Lahan",R.drawable.statuscolor));
    list.add(new timelinedetail_modul(3, "Video Proses Budidaya Cabai", R.drawable.statuscolor));
    list.add(new String("Pembersihan Rumput"));
    list.add(new timelinedetail_modul(4 ,"Video Pengendalian Gulma dengan Herbisida", R.drawable.statuscolororange));
    list.add(new timelinedetail_modul(5 ,"Informasi Rekomendasi saat Pengendalian", R.drawable.statuscolorgray));
    list.add(new timelinedetail_modul(6, "Artikel Pengendalian Gulma", R.drawable.statuscolorgray));

    listView.setAdapter(new adapterListview_detail(this, list));

这是适配器列表视图

 public View getView(int i, View view, ViewGroup viewGroup) {
    if(view == null) {
        switch (getItemViewType(i)){
            case JUDUL:
                view = inflater.inflate(R.layout.listview_detail, null);
                break;
            case HEADER:
                view = inflater.inflate(R.layout.listview_detail_header, null);
                break;
        }
    }

    switch (getItemViewType(i)){
        case JUDUL:


            ImageView image = (ImageView) view.findViewById(R.id.status);
            TextView judul = (TextView) view.findViewById(R.id.judul);
            TextView number = (TextView) view.findViewById(R.id.number);

            System.out.println(i);
            judul.setText(((timelinedetail_modul)list.get(i)).getJudul());
            number.setText((String.valueOf(((timelinedetail_modul) list.get(i)).getNumber())));
            image.setImageResource(((timelinedetail_modul)list.get(i)).getImage());

        break;
        case HEADER:

            TextView header = (TextView) view.findViewById(R.id.header);

            header.setText((String.valueOf(list.get(i))));

            break;
    }

    return view;


}

我使用硬编码列表视图构建它,所以如何使用我的JSON解析在listview中显示部分

更新:

我的json解析

` if(response.code() == 200){
                try {

                    JSONObject responseObject = new JSONObject(response.body().string());
                    JSONArray array = responseObject.getJSONArray("sections");

                    for (int i= 0 ; i<array.length() ; i ++) {

                        //sections
                        JSONObject sections = array.getJSONObject(i);
                        String nameSection = sections.getString("name");
                        String description = sections.getString("description");

                        ArrayList<Object> list = new ArrayList<>();


                            //topics
                            JSONArray topics = new JSONArray(array.getJSONObject(i).getString("topics"));

                            for (int j = 0; j <topics.length(); j++) {

                                String order = topics.getJSONObject(j).getString("order");
                                String nameTopics = topics.getJSONObject(j).getString("name");

                            }


                    }

                } catch (JSONException | IOException e) {
                    e.printStackTrace();
                }
            }`

示例我的JSON:

"sections": [ { "name": "Section 1", "description": "Persiapan Lahan", "createdAt": { "timestamp": 1487758752, "iso": "2017-02-22T10:19:12.264Z" }, "id": "5ae81880-f8e8-11e6-9d03-95a25b24350d", "topics": [ { "order": 1, "name": "Informasi Tantangan dan Kelengkapan Data Petani", "description": "Informasi Tantangan dan Kelengkapan Data Petani", "createdAt": { "timestamp": 1491551644, "iso": "2017-04-07T07:54:04.668Z" }, "id": "5ef38ad0-1b67-11e7-be66-8ff601a0bedd", "isCompleted": true, "status": "completed", "completedAt": { "timestamp": 1492489573, "iso": "2017-04-18T04:26:13.976Z" },

1 个答案:

答案 0 :(得分:0)

使用List(硬编码)创建适配器

adapterListview_detail adapter = new adapterListview_detail(this, mList);

解析Json之后,你应该更新你的列表(mList)并致电:

 adapter.notifyDataSetChanged();