【问题标题】:Show result JSON Parse to Custom Listview with Section将结果 JSON 解析显示到带有部分的自定义列表视图
【发布时间】:2017-05-15 04:52:00
【问题描述】:

我已经成功地在调试时解析我的 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 解析在列表视图中显示部分

更新:

我的 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" },

【问题讨论】:

  • 创建一个 java 类,在 json 文件中演示您的对象,并在解析过程中设置属性。然后列出这些对象并用它们填写您的列表。
  • 你能举例解释吗先生,对不起:(@nassertahani
  • 如果您放置 JSON 文件,我可以解释一下。
  • @nassertahani 编辑了我的问题。我显示我的 json 文件

标签: android json listview


【解决方案1】:

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

adapterListview_detail adapter = new adapterListview_detail(this, mList);

解析 Json 后,您应该更新您的列表 (mList) 并调用:

 adapter.notifyDataSetChanged();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-03-30
    • 1970-01-01
    • 1970-01-01
    • 2019-09-20
    • 1970-01-01
    • 2012-08-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多