【问题标题】:RecyclerView is not populating items whereas Adapter and ArrayList already has dataRecyclerView 没有填充项目,而 Adapter 和 ArrayList 已经有数据
【发布时间】:2019-01-14 07:42:49
【问题描述】:

recycerViewOrderNewItemofflineOrderProductListProductList 是两个recyclerviews,它们是在onCreate() 方法中初始化的。

        recycerViewOrderNewItem = findViewById(R.id.recycerViewOrderNewItem);
        recycerViewOrderNewItem.setLayoutManager(new LinearLayoutManager(getApplicationContext()));

        offlineOrderProductListProductList = findViewById(R.id.offlineOrderProductListProductList);
        offlineOrderProductListProductList.setLayoutManager(new LinearLayoutManager(getApplicationContext()));

下面是我以List<>检索我的数据的地方

    List<NewOrderEntryModel> allItemsOfOrder = new InitializeDatabase(OrderEntryActivity.this).myAppDatabaseInit.myDao().getAllNewOrderEntryModelByRefID(SalesID);

我正在为他们两个设置这样的适配器......

    offlineOrderProductListProductList.setAdapter(new NewOrderEntryAdapter(OrderEntryActivity.this, (ArrayList<NewOrderEntryModel>) allItemsOfOrder));

    recycerViewOrderNewItem.setAdapter(new NewOrderEntryAdapter(OrderEntryActivity.this, (ArrayList<NewOrderEntryModel>) allItemsOfOrder));

对于offlineOrderProductListProductList recyclerview 正在工作,但对于recycerViewOrderNewItem recyclerview 不工作

我已经调试了代码。 ArrayList 包含数据。

下面是我的适配器代码...

    public class NewOrderEntryAdapter extends RecyclerView.Adapter<NewOrderEntryAdapter.NewOrderEntryAdapterViewHolder>{

    private Context context;
    private ArrayList<NewOrderEntryModel> newOrderEntryModels;

    public NewOrderEntryAdapter(Context context, ArrayList<NewOrderEntryModel> newOrderEntryModels) {
        this.context = context;
        this.newOrderEntryModels = newOrderEntryModels;
    }

    @NonNull
    @Override
    public NewOrderEntryAdapterViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.list_item_order_entry_detail,parent,false);

        return new NewOrderEntryAdapterViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull NewOrderEntryAdapterViewHolder holder, final int position) {

        NewOrderEntryModel orderEntryModel = newOrderEntryModels.get(position);

        //Data

        final String name = orderEntryModel.getProductName();
        final String totalPrice = String.valueOf(orderEntryModel.getPBSalesTotal());
        final String code = String.valueOf(orderEntryModel.getPCode());
        final String quantity = String.valueOf(orderEntryModel.getPBInQty());
        final String price = String.valueOf(orderEntryModel.getPBSalesPrice());
        final String productID = String.valueOf(orderEntryModel.getPBProductID());


        // Binding
        holder.tvProductNameOrderEntry.setText(name);
        holder.tvProductTotalPriceOrderEntry.setText(totalPrice);
        holder.tvProductCodeOrderEntry.setText(code);
        holder.tvProductQuantityOrderEntry.setText(quantity);
        holder.tvProductPriceOrderEntry.setText(price);


        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                //Toast.makeText(context, "Reference id: "+orderEntryModel.getPBRefID()+" Table ID: "+orderEntryModel.getID(), Toast.LENGTH_SHORT).show();

                if(orderEntryModel.getPBRefID()==null){
                    //Toast.makeText(context, "Reference id: "+orderEntryModel.getPBRefID()+" Table ID: "+orderEntryModel.getID(), Toast.LENGTH_SHORT).show();
                    openDetailActivity(String.valueOf(position),"","",name,totalPrice,code,quantity,price,productID);
                }else {
                    Toast.makeText(context, "Reference id: "+orderEntryModel.getPBRefID()+" Table ID: "+orderEntryModel.getID(), Toast.LENGTH_SHORT).show();
                    openDetailActivity(String.valueOf(position),Integer.toString(orderEntryModel.getID()),orderEntryModel.getPBRefID(),name,totalPrice,code,quantity,price,productID);
                }

                //Toast.makeText(context, context.toString(), Toast.LENGTH_SHORT).show();

            }
        });

    }

    @Override
    public int getItemCount() {
        return newOrderEntryModels.size();
    }

    public class NewOrderEntryAdapterViewHolder extends RecyclerView.ViewHolder{

        public TextView tvProductNameOrderEntry
                ,tvProductTotalPriceOrderEntry
                ,tvProductCodeOrderEntry
                ,tvProductQuantityOrderEntry
                ,tvProductPriceOrderEntry;

        public NewOrderEntryAdapterViewHolder(View itemView) {
            super(itemView);

            tvProductNameOrderEntry = itemView.findViewById(R.id.tvProductNameOrderEntry);
            tvProductTotalPriceOrderEntry = itemView.findViewById(R.id.tvProductTotalPriceOrderEntry);
            tvProductCodeOrderEntry = itemView.findViewById(R.id.tvProductCodeOrderEntry);
            tvProductQuantityOrderEntry = itemView.findViewById(R.id.tvProductQuantityOrderEntry);
            tvProductPriceOrderEntry = itemView.findViewById(R.id.tvProductPriceOrderEntry);
        }
    }

    public void openDetailActivity(String position,
                                   String id,
                                   String pbRef,
                                   String productName,
                                   String totalPrice,
                                   String productCode,
                                   String quantity,
                                   String productPrice,
                                   String productID){

        Intent intent = new Intent(context, NewItemDetailActivity.class);
        intent.putExtra("position",position);
        intent.putExtra("id",id);
        intent.putExtra("pbRef",pbRef);
        intent.putExtra("productName",productName);
        intent.putExtra("totalPrice",totalPrice);
        intent.putExtra("productCode",productCode);
        intent.putExtra("quantity",quantity);
        intent.putExtra("productPrice",productPrice);
        intent.putExtra("productID",productID);

        context.startActivity(intent);
    }

}

请帮我解决这个问题...

【问题讨论】:

  • 请提供适配器代码。
  • 1.您是否检查了 recyclerview 的 xml 以确保您没有使用 match-parent 作为 recyclerview 项目的高度? 2. 从你的 recyclerview 适配器类调试代码,以确保 recyclerview 确实看到了数组
  • 通过评论每一个来检查recycleViews是否单独工作。如果这很可能是 XML height=match_parent 的问题。
  • 我总是将 wrap_content 用于 recyclerView。
  • 两个recyclerView是同一个页面?

标签: java android arraylist android-recyclerview


【解决方案1】:

我认为你应该清楚地初始化你的适配器和recyclerview。

allItemsOfOrder 可以像这样是全局的

List<NewOrderEntryModel> allItemsOfOrder = new ArrayList<>();

以下面的代码为例:

recyclerView = (RecyclerView) findViewById(R.id.recycerViewOrderNewItem);

    mAdapter = new NewOrderEntryAdapter(this,allItemsOfOrder);
    RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext());
    recyclerView.setLayoutManager(mLayoutManager);
    recyclerView.setItemAnimator(new DefaultItemAnimator());
    recyclerView.setAdapter(mAdapter);

那么你应该将订单添加到你的列表中

allItemsOfOrder 可以像这样是全局的

allItemsOfOrder.add(/*Something*/);

那么你应该像下面这样通知你的适配器......

mAdapter.notifyDataSetChanged();

您可以将此link 用作参考。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-09-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多