我在这里 GSON 用于 Json解析我正在获取我在[
{
"id": 1,
"warehouse_id": "1",
"name": "Brookline, MA",
"store_code": "Brookline",
"created_at": "2016-06-15 07:08:29",
"updated_at": null
},
{
"id": 3,
"warehouse_id": "3",
"name": "New Jersey, NJ",
"store_code": "New Jersey",
"created_at": "2016-06-15 07:14:35",
"updated_at": null
},
{
"id": 4,
"warehouse_id": "4",
"name": "Natick, MA",
"store_code": "Natick",
"created_at": "2016-06-15 07:14:55",
"updated_at": null
},
{
"id": 6,
"warehouse_id": "6",
"name": "Miami, FLG",
"store_code": "BBEV",
"created_at": "2016-06-15 07:15:19",
"updated_at": null
},......
中显示的数据列表,我想要添加一个自定义数据位于列表的第一位。
我的JSON:
ArrayList
问题:
如果我首先将自定义数据添加到
ArrayList
,然后我会解析 json,它覆盖了自定义数据&仅显示数据 来自API。如果我首先解析JSON&然后我将数据添加到
spinner
然后 注意到List<DashBoardSpinner> mList;
。
在GSON中,不需要创建ArrayList对象:
List<DashBoardSpinner> mList = new ArrayList<>();
但是我必须添加第一行自定义API数据,所以我创建了ArrayList的新对象。
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
public class DashBoardSpinner {
@SerializedName("id")
@Expose
private int id;
@SerializedName("warehouse_id")
@Expose
private String warehouseId;
@SerializedName("name")
@Expose
private String name;
@SerializedName("store_code")
@Expose
private String storeCode;
@SerializedName("created_at")
@Expose
private String createdAt;
@SerializedName("updated_at")
@Expose
private Object updatedAt;
public DashBoardSpinner(int id, String warehouse_id, String name, String store_code) {
this.id = id;
this.warehouseId = warehouse_id;
this.name = name;
this.storeCode = store_code;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getWarehouseId() {
return warehouseId;
}
public void setWarehouseId(String warehouseId) {
this.warehouseId = warehouseId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getStoreCode() {
return storeCode;
}
public void setStoreCode(String storeCode) {
this.storeCode = storeCode;
}
public String getCreatedAt() {
return createdAt;
}
public void setCreatedAt(String createdAt) {
this.createdAt = createdAt;
}
public Object getUpdatedAt() {
return updatedAt;
}
public void setUpdatedAt(Object updatedAt) {
this.updatedAt = updatedAt;
}
@Override
public String toString() {
return name;
}
}
DashBoardSpinner :( POJO班级)
List<DashBoardSpinner> mList = new ArrayList<>();
Gson gson = new Gson();
Type listType = new TypeToken<List<DashBoardSpinner>>() {
}.getType();
mList = gson.fromJson(response.toString(), listType);
Log.e("SIZE",""+mList.size());
Log.e("FirstItem",""+mList.get(0).getName());
//Custom Adding Data to ArrayList
DashBoardSpinner listFirst = new DashBoardSpinner(0,"0","All Warehouses","All Warehouses");
mList.add(1,listFirst);
片段类:
["B", "A", "C"]
请告诉我如何使用GSON使用API数据添加自己的自定义数据。 谢谢
答案 0 :(得分:2)
我不确定你是否能保证从gson获得一个ArrayList。
试试这个:
List<DashBoardSpinner> mList = new ArrayList<>();
Gson gson = new Gson();
Type listType = new TypeToken<List<DashBoardSpinner>>() {
}.getType();
List<DashboardSpinner> apiList = gson.fromJson(response.toString(), listType);
Log.e("SIZE",""+apiList.size());
Log.e("FirstItem",""+apiList.get(0).getName());
//Custom Adding Data to ArrayList
DashBoardSpinner listFirst = new DashBoardSpinner(0,"0","All Warehouses","All Warehouses");
mList.add(listFirst);
mList.addAll(apiList);