0

Я работаю с Android Recycler. Я пытаюсь сделать представление Recycler из API Response. Но список не появился, мой адаптер не работал. Когда я попытался отладить эту программу, я получил List<Produclist> list null.Сделайте Recyclerview Android от API, но он не работает

Это мой JSON ответ от API

{ 
    "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" 
} 

Это мой сеттер геттерного

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; 
    } 


} 

сеттер добытчик категории типа

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; 
     } 
} 

сеттер добытчик 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; 
    } 

} 

Это мой адаптер

public class ProductAdapter extends RecyclerView.Adapter<ProductAdapter.CatalogueHolder> { 

    private Context context; 
    private List<ProductList> list; 

    public ProductAdapter(Context context, List<ProductList> list) { 
     this.context = context; 
     this.list = list; 
    } 

    @Override 
    public CatalogueHolder onCreateViewHolder(ViewGroup parent, int viewType) { 
     View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.row_product, parent, false); 
     CatalogueHolder catalogueHolder = new CatalogueHolder(itemView); 
     return catalogueHolder; 
    } 

    @Override 
    public void onBindViewHolder(CatalogueHolder holder, int position) { 
     final ProductList item = list.get(position); 
     holder.itemView.setTag(item); 
     holder.productName.setText(item.getName()); 
    } 

    @Override 
    public int getItemCount() { 
     return list != null ? list.size() : 0; 
    } 

    public static class CatalogueHolder extends RecyclerView.ViewHolder { 

     @Bind(R.id.productname) 
     TextView productName; 
     @Bind(R.id.typeProduct) 
     TextView typeProduct; 
     @Bind(R.id.price) 
     TextView price; 
     @Bind(R.id.date) 
     TextView date; 

     public CatalogueHolder(View itemView) { 
      super(itemView); 
      ButterKnife.bind(this, itemView); 
     } 
    } 
} 

К сожалению, если это довольно долго. Я должен показать весь свой код, чтобы он дал понять. Мне очень нужна ваша помощь. Благодаря

ответ

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); 
        } 

       } 
      }); 
} 
+0

до сих пор не работает – Pandu