2017-08-22 30 views
0
"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,但我没有得到每个部分只有特定的关键数据。数据设置不正确在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); 
     } 
    } 
} 

输出什么,我m到处是:

enter image description here

,这就是I M预期输出:

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

我该如何设置每个标题部分的唯一相关事件?,帮助我..谢谢

回答

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大小

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

感谢兄弟..我忘了初始化列表对象。 –

1

您可以使用此方法解析您的资料希望这将帮助你

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

解析您的JSON数据和按日期使用ParseJson

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: enter image description here

+0

@SagarChavada您的欢迎 – akhilesh0707