sectionRecyclerview中的数据设置不正确

时间:2017-08-22 11:11:38

标签: android android-recyclerview

 "data": {
    "02-08-2017": [
      {
        "id": "5",
        "description": "testestet",
        "file": "http:\/\/ec2-35-166-240-31.us-west-2.compute.amazonaws.com\/uploads\/building\/395009\/documents\/doc_1059758282.pdf",
        "categoryname": "AGM",
        "category_id": "3",
        "created": "2017-08-02 00:00:00"
      },
      {
        "id": "8",
        "description": "test",
        "file": "http:\/\/ec2-35-166-240-31.us-west-2.compute.amazonaws.com\/uploads\/building\/395009\/documents\/doc_1109112346.pdf",
        "categoryname": "AGM",
        "category_id": "3",
        "created": "2017-08-02 00:00:00"
      }
    ],
    "31-07-2017": [
      {
        "id": "4",
        "description": "test ets",
        "file": "http:\/\/ec2-35-166-240-31.us-west-2.compute.amazonaws.com\/uploads\/building\/395009\/documents\/doc_804913596.pdf",
        "categoryname": "Safety and security",
        "category_id": "4",
        "created": "2017-07-31 00:00:00"
      }
    ],
    "30-07-2017": [
      {
        "id": "3",
        "description": "AGM meetings note detail",
        "file": "http:\/\/ec2-35-166-240-31.us-west-2.compute.amazonaws.com\/uploads\/building\/395009\/documents\/doc_1130546608.pdf",
        "categoryname": "AGM",
        "category_id": "3",
        "created": "2017-07-30 00:00:00"
      }
    ],
    "30-06-2017": [
      {
        "id": "6",
        "description": "test estet",
        "file": "http:\/\/ec2-35-166-240-31.us-west-2.compute.amazonaws.com\/uploads\/building\/395009\/documents\/doc_460968130.pdf",
        "categoryname": "Safety and security",
        "category_id": "4",
        "created": "2017-06-30 00:00:00"
      }
    ],
    "31-05-2017": [
      {
        "id": "7",
        "description": "test",
        "file": "http:\/\/ec2-35-166-240-31.us-west-2.compute.amazonaws.com\/uploads\/building\/395009\/documents\/doc_1862829755.pdf",
        "categoryname": "AGM",
        "category_id": "3",
        "created": "2017-05-31 00:00:00"
      }
    ]
  }

我将这个json对象设置为sectionRecyclerView,但我没有在每个部分中获得特定的关键数据。

private void parseJson(JSONObject data) {
        if (data != null) {
            Iterator<String> it = data.keys();
            while (it.hasNext()) {
                key = it.next();
                Log.d("Key:", key);
                Document document = new Document();
                document.setDate(key);
                try {
                    if (data.get(key) instanceof JSONArray) {
                        JSONArray arry = data.getJSONArray(key);
                        int size = arry.length();
                        for (int i = 0; i < size; i++) {
                            DocumentFiles files = new DocumentFiles();
                            JSONObject obj = arry.getJSONObject(i);
                            files.setFileName(obj.getString("description"));
                            mDocumentFiles.add(files);
                            document.setmList(mDocumentFiles);
                            Log.d("Description:",obj.getString("description"));
                        }

                        Log.d("Stop:", "Yes");
                        mDocumentList.add(document);
                        adapterDocument.addSection(new MySection(key, mDocumentList, mDocumentFiles));
                    } else if (data.get(key) instanceof JSONObject) {

                    } else {
                        System.out.println("" + key + " : " + data.optString(key));
                    }
                } catch (Throwable e) {
                    System.out.println("" + key + " : " + data.optString(key));
                    e.printStackTrace();
                }
            }
        }
        adapterDocument.notifyDataSetChanged();
    }

ViewHolder类

public class MySection extends StatelessSection {

    String date;
    List<Document> documentList;
    List<DocumentFiles> documentFileList;


    public MySection(String date, List<Document> callList, List<DocumentFiles> documentFileList) {
        // call constructor with layout resources for this Section header, footer and items 
        super(new SectionParameters.Builder(R.layout.section_item)
                .headerResourceId(R.layout.section_header)
                .build());
        this.date = date;
        this.documentList = callList;
        this.documentFileList = documentFileList;
    }

    @Override
    public int getContentItemsTotal() {
        return documentList.size(); // number of items of this section
    }

    @Override
    public RecyclerView.ViewHolder getItemViewHolder(View view) {
        // return a custom instance of ViewHolder for the items of this section
        return new MyItemViewHolder(view);
    }

    @Override
    public void onBindItemViewHolder(RecyclerView.ViewHolder holder, int position) {
        MyItemViewHolder itemHolder = (MyItemViewHolder) holder;
        DocumentFiles files = documentFileList.get(position);
        itemHolder.txtFile.setText(files.getFileName());

        // itemHolder.txtFile.setText(documentList.get(position).getFileName());
    }

    @Override
    public RecyclerView.ViewHolder getHeaderViewHolder(View view) {
        return new MyHeaderViewHolder(view);
    }

    @Override
    public void onBindHeaderViewHolder(RecyclerView.ViewHolder holder) {
        MyHeaderViewHolder headerHolder = (MyHeaderViewHolder) holder;
        headerHolder.txtDate.setText(date);
    }

    class MyItemViewHolder extends RecyclerView.ViewHolder {
        private final TextView txtFile;

        public MyItemViewHolder(View itemView) {
            super(itemView);
            txtFile = (TextView) itemView.findViewById(R.id.txtFname);
        }
    }

    class MyHeaderViewHolder extends RecyclerView.ViewHolder {
        private final TextView txtDate;

        public MyHeaderViewHolder(View itemView) {
            super(itemView);
            txtDate = (TextView) itemView.findViewById(R.id.txtDate);
        }
    }
}

输出我得到的是:

enter image description here

这是我期待的输出:

20-08-2017
   testestet
   test
31-07-2017
   test ets
30-07-2017
   AGM meetings note detail
30-06-2017
   test

我如何只为每个标题部分设置特定事件?,帮助我..谢谢

2 个答案:

答案 0 :(得分:1)

问题是您创建List<DocumentFiles>并提供对每个项目的引用,因此每个项目都将获取完整的列表对象。

尝试为每个文档创建List<DocumentFiles>并将其设置为每个文档,它将正常工作。

private void parseJson(JSONObject data) {
    if (data != null) {
        Iterator<String> it = data.keys();
        while (it.hasNext()) {
            key = it.next();
            Log.d("Key:", key);
            Document document = new Document();
            mDocumentFiles = new ArrayList<DocumentFiles>(); // create everytime new list for each header
            document.setDate(key);
            try {
                if (data.get(key) instanceof JSONArray) {
                    JSONArray arry = data.getJSONArray(key);
                    int size = arry.length();
                    for (int i = 0; i < size; i++) {
                        DocumentFiles files = new DocumentFiles();
                        JSONObject obj = arry.getJSONObject(i);
                        files.setFileName(obj.getString("description"));
                        mDocumentFiles.add(files);
                        //document.setmList(mDocumentFiles); moved out of the loop
                        Log.d("Description:",obj.getString("description"));
                    }

                    Log.d("Stop:", "Yes");
                    document.setmList(mDocumentFiles);
                    mDocumentList.add(document);
                    // No use of mDocumentList, you are using only mDocumentFiles
                    adapterDocument.addSection(new MySection(key, mDocumentList, mDocumentFiles));
                } else if (data.get(key) instanceof JSONObject) {

                } else {
                    System.out.println("" + key + " : " + data.optString(key));
                }
            } catch (Throwable e) {
                System.out.println("" + key + " : " + data.optString(key));
                e.printStackTrace();
            }
        }
    }
    adapterDocument.notifyDataSetChanged();
}

您的适配器大小应为documentFileList size

@Override
public int getContentItemsTotal() {
    return documentFileList.size(); // number of documentFileList items of this section
}

答案 1 :(得分:1)

您可以使用此方法解析数据,希望这可以帮助您

Map<String, List<DocumentChild>> parseData = parseJson(data);
for (Map.Entry<String, List<DocumentChild>> entry : parseData.entrySet()) {
    Log.d(TAG, entry.getKey() + "/" + entry.getValue());
}

使用ParseJson

解析您的JSON数据和按日期分组
private Map<String, List<DocumentChild>> parseJson(String data) {
    Map<String, List<DocumentChild>> dataMap = new HashMap<>();
    try {
        JSONObject jsonObj = new JSONObject(data);
        JSONObject jsonObject = jsonObj.getJSONObject("data");
        Iterator<String> it = jsonObject.keys();
        while (it.hasNext()) {
            String strKey = it.next();
            JSONArray jsonArrayDate = jsonObject.getJSONArray(strKey);
            for (int i = 0; i < jsonArrayDate.length(); i++) {
                JSONObject jsonObjectData = jsonArrayDate.getJSONObject(i);
                DocumentChild documentChild = new DocumentChild();
                documentChild.setId(jsonObjectData.getString("id"));
                documentChild.setDescription(jsonObjectData.getString("description"));
                documentChild.setFile(jsonObjectData.getString("file"));
                documentChild.setCategoryName(jsonObjectData.getString("categoryname"));
                documentChild.setCategoryId(jsonObjectData.getString("category_id"));
                documentChild.setCreatedDate(jsonObjectData.getString("created"));

                if (dataMap.containsKey(strKey)) {
                    // The key is already in the HashMap; add the pojo object
                    // against the existing key.
                    dataMap.get(strKey).add(documentChild);
                } else {
                    List list = new ArrayList();
                    list.add(documentChild);
                    dataMap.put(strKey, list);
                }
            }
        }
    } catch (JSONException e) {
        Log.d(TAG, "Exception catched: " + e);
        e.printStackTrace();
    }

    return dataMap;
}

DocumentChild

public class DocumentChild {
    private String id;
    private String description;
    private String file;
    private String categoryName;
    private String categoryId;
    private String createdDate;

   //TODO generate getter setter

}

你的愿望OP: this great book