Recyclerview android,我的适配器无法正常工作

时间:2017-02-06 03:22:55

标签: android android-recyclerview recycler-adapter

我制作了一个回收机安卓程序,但列表并没有显示出来。我认为我的适配器出了问题。如果你们需要,我会展示JSON或我的二传手吸气剂。

这是我的活动

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 个答案:

答案 0 :(得分:2)

您的public List<ProductList> list; null

你应该如下声明列表变量:

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

在您的onNext()中,您应填写回复中的列表并致电adapter.notifyDataSetChanged()

答案 1 :(得分:0)

在请求方法中放入onnext: 得到回应并添加到列表

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

进入活动声明

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