首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在回收器视图中不同行显示嵌套数组

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

Stack Overflow用户
提问于 2021-06-12 02:18:27
回答 2查看 26关注 0票数 0

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

代码语言:javascript
复制
 {"posts": [
     {
       "id": 1,
       "ledger": "ledger1",
       "credit" : "343"
       "item": [
         {"id":10,
          "rate": 5,
          "qty": 50,
          "total": 250
         },
         {"id":12,
          "rate": 50,
          "qty": 50,
          "total": 2500
         }
       ] 
     },
     {
       "id": 2,
       "ledger": "ledger2",
       "credit" : "343"
       "item": [
         {"id":10,
          "rate": 5,
          "qty": 50,
          "total": 250
         },
         {"id":42,
          "rate": 50,
          "qty": 50,
          "total": 2500
         },
         {"id":17,
          "rate": 50,
          "qty": 50,
          "total": 2500
         },
         {"id":12,
          "rate": 50,
          "qty": 50,
          "total": 2500
         }
       ] 
     },
     {
       "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

我想使回收商视图,其中将显示的数据是这个格式的账本和他们的相应项目

提前谢谢你

EN

回答 2

Stack Overflow用户

发布于 2021-07-08 13:05:53

使用两个RecyclerView;

  • First RecyclerView with post RecyclerView RecyclerView with post.item ViewHolder。

另一种方式是使用epoxy

票数 0
EN

Stack Overflow用户

发布于 2021-07-08 15:34:51

您可以使用特定的数据结构将其保留在一个数据中。您可以使用1个recyclerView而不是2个recyclerView来实现此功能。无论如何,2 recyclerView永远不是推荐的方法。

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

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

模型或对象类可以如下所示:

代码语言:javascript
复制
-----------------------------------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的输入

您需要将Post mapLedgerItem对象。

当您开始循环遍历项目时,每个Post对象都将使用ledgerViewType作为

  • ,您将添加一个包含标题行并将ledgerViewType作为LEDGER_HEADERLedgerItem。你可以通过创建一个更好的布局来跳过这一步,它以适当的方式标记每一项。查看此屏幕截图。

作为TRANSACTION_ITEM.的

  1. 每个Item对象都将使用ledgerViewType

您可以循环遍历这些项,并继续添加所有项。你会有一个扁平化的列表。

你可以参考这个答案来获得更多的解释。https://stackoverflow.com/a/40570333/4491971

代码语言:javascript
复制
    public class LedgerItem {
        private String uniqueId;
        private Post post;
        private Item item;
        private int ledgerViewType;
    }
代码语言:javascript
复制
    public static enum LedgerViewType {
        LEDGER_MAIN(100),
        TRANSACTION_ITEM(200),
        LEDGER_HEADER(300);
    }

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67941828

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档