2017-02-06 195 views
0

我做了一個回收站android,但是列表沒有顯示出來。我認爲我的適配器有一個錯誤。如果你們需要他們,我會展示JSON或者我們的setter getter。Recyclerview android,我的適配器不工作

這我的活動

public class ProductActivity extends AppCompatActivity { 

    private RecyclerView rvView; 
    private RecyclerView.Adapter adapter; 
    private RecyclerView.LayoutManager layoutManager; 
    public List<ProductList> list; 
    Context context; 
    public static final String MyPREFERENCES = "MyPrefs" ; 

    @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) { 
         } 

        } 
       }); 
    } 

    public ProductListRequest constructSignUpRequest() { 
     ProductListRequest request = new ProductListRequest(); 
     SharedPreferences prefs = getSharedPreferences(MyPREFERENCES, MODE_PRIVATE); 
     request.setCode(prefs.getString(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); 
     } 
    } 
} 

我試着調試這個程序,並得到了List<ProductList> list是空

+2

你只是回答你的問題,你的列表是空的,因爲你沒有初始化它。 – Enzokie

+0

如何初始化它?對不起,我在開發Android – Pandu

回答

2

public List<ProductList> list;

應聲明如下列表變量:

private List<ProductList> list = new ArrayList<>(); 

,並在您onNext()你應該從響應填滿你的清單,並呼籲adapter.notifyDataSetChanged()

+0

新應該在哪裏放這個變量?在我的適配器或我的活動? – Pandu

+0

把這個放在你的活動 –

+0

我已經把它放在我的活動中,而我的適配器仍然是空的 – Pandu

0

在投入onnext請求方法: 得到響應並加入清單

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

活動申報

private List<ProductList> list = new ArrayList<>(); 
+0

我試過這個,但recyclerview是空對象參考 – Pandu

+0

從logcat複製和過去的錯誤 – KMI

+0

這是錯誤嘗試在null對象參考 – Pandu

相關問題