我有给定形式的数据,我想在回收视图中显示它,因为你可以看到每个账本都有不同数量的项目。我想用账本显示这些项目,在这种形式下,我在下面提到,请给我代码,如果可能的话,我是新的android。
{"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我想使回收商视图,其中将显示的数据是这个格式的账本和他们的相应项目
提前谢谢你
发布于 2021-07-08 13:05:53
使用两个RecyclerView;
RecyclerView with post RecyclerView RecyclerView with post.item ViewHolder。另一种方式是使用epoxy。
发布于 2021-07-08 15:34:51
您可以使用特定的数据结构将其保留在一个数据中。您可以使用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,
"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": [
]
}
]
}模型或对象类可以如下所示:
-----------------------------------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 map到LedgerItem对象。
当您开始循环遍历项目时,每个Post对象都将使用ledgerViewType作为
ledgerViewType作为LEDGER_HEADER的LedgerItem。你可以通过创建一个更好的布局来跳过这一步,它以适当的方式标记每一项。查看此屏幕截图。
作为TRANSACTION_ITEM.的
Item对象都将使用ledgerViewType您可以循环遍历这些项,并继续添加所有项。你会有一个扁平化的列表。
你可以参考这个答案来获得更多的解释。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读取数据,依此类推。
https://stackoverflow.com/questions/67941828
复制相似问题