ArrayList<String> 使用改造 android 解析成 recyclerview

Posted

技术标签:

【中文标题】ArrayList<String> 使用改造 android 解析成 recyclerview【英文标题】:ArrayList<String> parse into recyclerview with retrofit android 【发布时间】:2019-11-11 20:12:30 【问题描述】:

您好,我有一个JSON,但它是ArrayList&lt;String&gt; 的数组我对这种类型的数组感到困惑我如何使用Retrofit 将这些类型的数组解析到我的recyclerview


  "statusCode": 200,
  "success": true,
  "message": "Data get succesfully.",
  "data": [
    
      "attributes_id": 20,
      "category_id": "3",
      "subcategory_id": "13",
      "product_id": "28",
      "attribute_name": "Make",
      "isRequired": "yes",
      "attribute_type": "dropdown",
      "attribute_options": [
        "LG",
        "Samsung",
        "Kelvinetor",
        "Haier",
        "Wrilphool",
        "Other"
      ],
      "created_at": "2019-07-01 12:02:18",
      "updated_at": "2019-07-01 12:02:57"
    
  ]

这是我的json,我尝试将attribute_options 列入Recyclerview。 帮帮我。

这是我的回应:

public class GetAttributesResponse 

@SerializedName("statusCode")
@Expose
private int statusCode;

@SerializedName("success")
@Expose
private boolean success;

@SerializedName("message")
@Expose
private String message;

@SerializedName("data")
@Expose
private ArrayList<DataClass> dataclass = null;

public int getStatusCode() 
    return statusCode;


public void setStatusCode(int statusCode) 
    this.statusCode = statusCode;


public boolean isSuccess() 
    return success;


public void setSuccess(boolean success) 
    this.success = success;


public String getMessage() 
    return message;


public void setMessage(String message) 
    this.message = message;


public ArrayList<DataClass> getDataclass() 
    return dataclass;


public void setDataclass(ArrayList<DataClass> dataclass) 
    this.dataclass = dataclass;


public class DataClass 

    @SerializedName("attributes_id")
    @Expose
    private int attributes_id;

    @SerializedName("category_id")
    @Expose
    private String category_id;

    @SerializedName("subcategory_id")
    @Expose
    private String subcategory_id;

    @SerializedName("product_id")
    @Expose
    private String product_id;

    @SerializedName("attribute_name")
    @Expose
    private String attribute_name;

    @SerializedName("isRequired")
    @Expose
    private String isRequired;

    @SerializedName("attribute_type")
    @Expose
    private String attribute_type;

    @SerializedName("created_at")
    @Expose
    private String created_at;

    @SerializedName("updated_at")
    @Expose
    private String updated_at;

    @SerializedName("attribute_options")
    @Expose
    private ArrayList<String> attribute_options = null;

    public int getAttributes_id() 
        return attributes_id;
    

    public void setAttributes_id(int attributes_id) 
        this.attributes_id = attributes_id;
    

    public String getCategory_id() 
        return category_id;
    

    public void setCategory_id(String category_id) 
        this.category_id = category_id;
    

    public String getSubcategory_id() 
        return subcategory_id;
    

    public void setSubcategory_id(String subcategory_id) 
        this.subcategory_id = subcategory_id;
    

    public String getProduct_id() 
        return product_id;
    

    public void setProduct_id(String product_id) 
        this.product_id = product_id;
    

    public String getAttribute_name() 
        return attribute_name;
    

    public void setAttribute_name(String attribute_name) 
        this.attribute_name = attribute_name;
    

    public String getIsRequired() 
        return isRequired;
    

    public void setIsRequired(String isRequired) 
        this.isRequired = isRequired;
    

    public String getAttribute_type() 
        return attribute_type;
    

    public void setAttribute_type(String attribute_type) 
        this.attribute_type = attribute_type;
    

    public String getCreated_at() 
        return created_at;
    

    public void setCreated_at(String created_at) 
        this.created_at = created_at;
    

    public String getUpdated_at() 
        return updated_at;
    

    public void setUpdated_at(String updated_at) 
        this.updated_at = updated_at;
    

    public ArrayList<String> getAttribute_options() 
        return attribute_options;
    

    public void setAttribute_options(ArrayList<String> attribute_options) 
        this.attribute_options = attribute_options;
    

【问题讨论】:

请在收到 json 数据的地方添加改造代码。 你有没有为改造使用模型类 @Antonio 请查看我更新我的问题并添加我的回复 @UmarHussain 我添加了我的响应代码,请查看。 @HitechP,你能显示你收到响应的代码吗? 【参考方案1】:

首先,您需要使用Class Generator 创建 JSON 到 POJO 类。

package com.test.example;

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

public class Datum 

@SerializedName("attributes_id")
@Expose
private Integer attributesId;
@SerializedName("category_id")
@Expose
private String categoryId;
@SerializedName("subcategory_id")
@Expose
private String subcategoryId;
@SerializedName("product_id")
@Expose
private String productId;
@SerializedName("attribute_name")
@Expose
private String attributeName;
@SerializedName("isRequired")
@Expose
private String isRequired;
@SerializedName("attribute_type")
@Expose
private String attributeType;
@SerializedName("attribute_options")
@Expose
private List<String> attributeOptions = null;
@SerializedName("created_at")
@Expose
private String createdAt;
@SerializedName("updated_at")
@Expose
private String updatedAt;

public Integer getAttributesId() 
return attributesId;


public void setAttributesId(Integer attributesId) 
this.attributesId = attributesId;


public String getCategoryId() 
return categoryId;


public void setCategoryId(String categoryId) 
this.categoryId = categoryId;


public String getSubcategoryId() 
return subcategoryId;


public void setSubcategoryId(String subcategoryId) 
this.subcategoryId = subcategoryId;


public String getProductId() 
return productId;


public void setProductId(String productId) 
this.productId = productId;


public String getAttributeName() 
return attributeName;


public void setAttributeName(String attributeName) 
this.attributeName = attributeName;


public String getIsRequired() 
return isRequired;


public void setIsRequired(String isRequired) 
this.isRequired = isRequired;


public String getAttributeType() 
return attributeType;


public void setAttributeType(String attributeType) 
this.attributeType = attributeType;


public List<String> getAttributeOptions() 
return attributeOptions;


public void setAttributeOptions(List<String> attributeOptions) 
this.attributeOptions = attributeOptions;


public String getCreatedAt() 
return createdAt;


public void setCreatedAt(String createdAt) 
this.createdAt = createdAt;


public String getUpdatedAt() 
return updatedAt;


public void setUpdatedAt(String updatedAt) 
this.updatedAt = updatedAt;




package com.test.example;

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

public class FeasibilityModel 

@SerializedName("statusCode")
@Expose
private Integer statusCode;
@SerializedName("success")
@Expose
private Boolean success;
@SerializedName("message")
@Expose
private String message;
@SerializedName("data")
@Expose
private List<Datum> data = null;

public Integer getStatusCode() 
return statusCode;


public void setStatusCode(Integer statusCode) 
this.statusCode = statusCode;


public Boolean getSuccess() 
return success;


public void setSuccess(Boolean success) 
this.success = success;


public String getMessage() 
return message;


public void setMessage(String message) 
this.message = message;


public List<Datum> getData() 
return data;


public void setData(List<Datum> data) 
this.data = data;



改造示例

 @GET("//Your End Point URL")
    Call<Datum> getList();

您的回复示例

call.enqueue(new Callback<List<Hero>>() 
            @Override
            public void onResponse(Call<Datum> call, Response<Datum> response) 

                //In this point we got our hero list
                //thats damn easy right ;) 

//now we can do whatever we want with this list

            

            @Override
            public void onFailure(Call<Datum> call, Throwable t) 
                Toast.makeText(getApplicationContext(), t.getMessage(), Toast.LENGTH_SHORT).show();
            
        );

参考链接:https://www.simplifiedcoding.net/retrofit-android-example/

【讨论】:

感谢您的重播,但这里我只有数组键而不是更详细的值虎钳数据

以上是关于ArrayList<String> 使用改造 android 解析成 recyclerview的主要内容,如果未能解决你的问题,请参考以下文章

在 Java 中将 'ArrayList<String> 转换为 'String[]'

在 Java 中将 'ArrayList<String> 转换为 'String[]'

将 ArrayList<String> 转换为 String[] [重复]

ArrayList如何通过循环动态添加另一个ArrayList,请帮帮忙!

是否可以使用 Gson.fromJson() 来获取 ArrayList<ArrayList<String>>?

ArrayList<HashMap<String,String>> 到 JSON 对象 Java