如何在不同行的回收站视图中显示嵌套数组

问题描述

我有给定形式的数据,我想在回收站视图中显示它,因为您可以看到每个分类帐都有不同数量的项目。我想用分类显示这些项目,以我在下面提到的这种形式,如果可能,请给我代码,我是 android 新手。

 {"posts": [
     {
       "id": 1,"ledger": "ledger1","credit" : "343"
       "item": [
         {"id":10,"rate": 5,"qty": 50,"total": 250
         },{"id":12,"rate": 50,"total": 2500
         }
       ] 
     },{
       "id": 2,"ledger": "ledger2",{"id":42,"total": 2500
         },{"id":17,{
       "id": 3,"ledger": "ledger","credit" : "1000","item": []
    ]}

**id   ledger     credit**
--------------------------
1      ledger1    343
*id   iRate   qty   total*
10    5       50   250
12    50      50   2500
_________________________
2     ledger2    343
*id   iRate   qty   total*
10    5       50   250
42    50      50   2500
12    50      50   2500
17    50      50   2500
__________________________
3     ledger3    1000

我想制作回收器视图,它将显示数据是分类帐及其相应项目的这种格式
提前谢谢你

解决方法

使用两个 RecyclerView;

  • 第一个 RecyclerViewpost ViewHolder。
  • 第二个 RecyclerView,带有 post.item ViewHolder。

另一种方式,使用 epoxy

,

您可以使用特定的数据结构将其保留在一个数据中。您可以仅使用 1 个 recyclerView 而不是 2 个 recyclerView 来实现这一点。无论如何 2 recyclerView 从来都不是建议的方法。

JSON 有一些格式问题。这是更新后的 JSON -

{
  "posts": [
    {
      "id": 1,"ledger": "ledger1","credit": "343","item": [
        {
          "id": 10,"rate": 5,"qty": 50,"total": 250
        },{
          "id": 12,"rate": 50,"total": 2500
        }
      ]
    },{
      "id": 2,"ledger": "ledger2",{
          "id": 42,"total": 2500
        },{
          "id": 17,{
      "id": 3,"ledger": "ledger","credit": "1000","item": [
        
      ]
    }
  ]
}

模型或对象类可以如下:

-----------------------------------com.example.Item.java-----------------------------------

package com.example;

import javax.annotation.Generated;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

@Generated("jsonschema2pojo")
public class Item {

@SerializedName("id")
@Expose
private Integer id;
@SerializedName("rate")
@Expose
private Integer rate;
@SerializedName("qty")
@Expose
private Integer qty;
@SerializedName("total")
@Expose
private Integer total;

public Integer getId() {
return id;
}

public void setId(Integer id) {
this.id = id;
}

public Integer getRate() {
return rate;
}

public void setRate(Integer rate) {
this.rate = rate;
}

public Integer getQty() {
return qty;
}

public void setQty(Integer qty) {
this.qty = qty;
}

public Integer getTotal() {
return total;
}

public void setTotal(Integer total) {
this.total = total;
}

}
-----------------------------------com.example.Post.java-----------------------------------

package com.example;

import java.util.List;
import javax.annotation.Generated;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

@Generated("jsonschema2pojo")
public class Post {

@SerializedName("id")
@Expose
private Integer id;
@SerializedName("ledger")
@Expose
private String ledger;
@SerializedName("credit")
@Expose
private String credit;
@SerializedName("item")
@Expose
private List<Item> item = null;

public Integer getId() {
return id;
}

public void setId(Integer id) {
this.id = id;
}

public String getLedger() {
return ledger;
}

public void setLedger(String ledger) {
this.ledger = ledger;
}

public String getCredit() {
return credit;
}

public void setCredit(String credit) {
this.credit = credit;
}

public List<Item> getItem() {
return item;
}

public void setItem(List<Item> item) {
this.item = item;
}

}
-----------------------------------com.example.PostsResponse.java-----------------------------------

package com.example;

import java.util.List;
import javax.annotation.Generated;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

@Generated("jsonschema2pojo")
public class PostsResponse {

@SerializedName("posts")
@Expose
private List<Post> posts = null;

public List<Post> getPosts() {
return posts;
}

public void setPosts(List<Post> posts) {
this.posts = posts;
}

}

数据结构可以如下:

List<LedgerItem> - 作为列表输入到 RecyclerView

您需要将 map PostLedgerItem 对象。

  1. 每个 Post 对象都将 ledgerViewType 作为 LEDGER_MAIN
  2. 每次开始循环遍历项目时,都会添加一个 LedgerItem,它具有标题行,并将 ledgerViewType 作为 LEDGER_HEADER。您可以通过创建更好的布局来跳过此步骤,该布局以适当的方式标记每个项目。检查此屏幕截图。 enter image description here
  3. 每个 Item 对象都将 ledgerViewType 作为 TRANSACTION_ITEM

您可以遍历项目并继续添加所有项目。您将拥有一个扁平化列表。

您可以参考此答案以获取更多解释。 https://stackoverflow.com/a/40570333/4491971

    public class LedgerItem {
        private String uniqueId;
        private Post post;
        private Item item;
        private int ledgerViewType;
    }

    public static enum LedgerViewType {
        LEDGER_MAIN(100),TRANSACTION_ITEM(200),LEDGER_HEADER(300);
    }

每个 PostViewHolder 都可以有项目列表。因此,您可以使用 onBindViewHolder 加载项目。根据 ViewType,您可以让 ViewHolders 相应地处理数据显示。例如,PostViewHolder 将从 ledgerItem.post 读取数据等等。