从API制作Recyclerview Android,但它不起作用

时间:2017-02-05 15:39:37

标签: android android-recyclerview observable recycler-adapter

我一直在使用Recycler查看Android。我尝试从API响应中创建一个Recycler视图。但是,列表没有显示,我的适配器无法正常工作。当我试图调试这个程序时,我得到6为空。

这是我对API的JSON响应

List<Produclist> list

这是我的二传手吸气器

{
  "code": 0,
  "data": [
    {
      "product_list": [
        {
          "return_level": 0,
          "image": "ee0c97c5-1752-4f1a-b713-2308eb1284d8",
          "risk_level": "Aggresive",
          "code": "P0011",
          "price": {
            "date": null,
            "value": 0
          },
          "name": "Dana Ekuitas Prima"
        },
        {
          "return_level": 0,
          "image": "ee0c97c5-1752-4f1a-b713-2308eb1284d8",
          "risk_level": "Aggresive",
          "code": "P0001",
          "price": {
            "date": "2017-01-03T11:44:52.6152Z",
            "value": 150000
          },
          "name": "Manulife Dana Saham"
        },
        {
          "return_level": 0,
          "image": "ee0c97c5-1752-4f1a-b713-2308eb1284d8",
          "risk_level": "Aggresive",
          "code": "P0008",
          "price": {
            "date": null,
            "value": 0
          },
          "name": "Trim Kapital Plus"
        },
        {
          "return_level": 0,
          "image": "ee0c97c5-1752-4f1a-b713-2308eb1284d8",
          "risk_level": "Aggresive",
          "code": "P0004",
          "price": {
            "date": "2017-01-03T11:44:52.6152Z",
            "value": 150000
          },
          "name": "Manulife Syariah Sektoral Amanah"
        }
      ],
      "type": "Reksa Dana Saham"
    }
  ],
  "info": "Product list successfully loaded"
}

类别类型的Setter getter

public class ProductListResponse {

    @SerializedName("code")
    @Expose
    private Integer code;
    @SerializedName("info")
    @Expose
    private String info;
    @SerializedName("data")
    @Expose
    private List<CategoryType> listCategory = new ArrayList<>();

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public String getInfo() {
        return info;
    }


    public void setInfo(String info) {
        this.info = info;
    }

    public List<CategoryType> getListCategory() {
        return listCategory;
    }


    public void setListCategory(List<CategoryType> listCategory) {
        this.listCategory = listCategory;
    }


}

ProductList的Setter getter

    public class CategoryType {

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

        @SerializedName("product_list")
        @Expose
        private List<ProductList> productList = new ArrayList<ProductList>();

        public String getType() {
            return type;
        }

        public void setType(String type) {
            this.type = type;
        }

        public List<ProductList> getProductList() {
            return productList;
        }

        public void setProductList(List<ProductList> productList) {
            this.productList = productList;
        }
}

这是我的活动

public class ProductList implements Serializable {

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

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

    @SerializedName("price")
    @Expose
    private Price price;

    @SerializedName("risk_level")
    @Expose
    private String riskLevel;

    @SerializedName("return_level")
    @Expose
    private Double returnLevel;

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

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getRiskLevel() {
        return riskLevel;
    }

    public void setRiskLevel(String riskLevel) {
        this.riskLevel = riskLevel;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }


    public Price getPrice() {
        return price;
    }

    public void setPrice(Price price) {
        this.price = price;
    }

    public Double getreturnLevel() {
        return returnLevel;
    }

    public void setReturnLevel(Double returnLevel) {
        this.returnLevel = returnLevel;
    }

    public String getImage() {
        return image;
    }

    public void setImage(String image) {
        this.image = image;
    }

这是我的适配器

public class ProductActivity extends AppCompatActivity {

    private RecyclerView rvView;
    private RecyclerView.Adapter adapter;
    private RecyclerView.LayoutManager layoutManager;
    public List<ProductList> list;
    Context context;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.f_list_of_product);
        request(constructSignUpRequest());

        rvView = (RecyclerView) findViewById(R.id.rv);
        rvView.setHasFixedSize(true);
        layoutManager = new LinearLayoutManager(this);

        rvView.setLayoutManager(layoutManager);
        adapter = new ProductAdapter(context, list);
        rvView.setAdapter(adapter);
    }


    public BTNService.Api getApi() {
        return getBTNService().getApi();
    }

    public BTNService getBTNService() {
        return new BTNService(this);
    }

    void request(final ProductListRequest productListRequest) {
        getApi().loadproductlist(productListRequest.getCode())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(new Observer<ProductListResponse>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        Timber.e(e.getLocalizedMessage());
                    }

                    @Override
                    public void onNext(ProductListResponse response) {
                        if (response != null) {
                            Intent intent = new Intent(ProductActivity.this, ProductList.class);
                            startActivity(intent);
                        }

                    }
                });
    }

    public ProductListRequest constructSignUpRequest() {
        ProductListRequest request = new ProductListRequest();
        request.setCode(Category);
        return request;
    }

}

抱歉,如果它很长。我必须展示我的所有代码才能说清楚。我真的需要你的帮助。感谢

1 个答案:

答案 0 :(得分:0)

在onComplete方法中设置适配器:

void request(final ProductListRequest productListRequest) {
    getApi().loadproductlist(productListRequest.getCode())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribeOn(Schedulers.io())
            .subscribe(new Observer<ProductListResponse>() {
                @Override
                public void onCompleted() {

                rvView.setHasFixedSize(true);
                layoutManager = new LinearLayoutManager(this);

                rvView.setLayoutManager(layoutManager);
                adapter = new ProductAdapter(context, list);
                rvView.setAdapter(adapter);

                }

                @Override
                public void onError(Throwable e) {
                    Timber.e(e.getLocalizedMessage());
                }

                @Override
                public void onNext(ProductListResponse response) {
                    if (response != null) {
                        Intent intent = new Intent(ProductActivity.this, ProductList.class);
                        startActivity(intent);
                    }

                }
            });
}