Wasi Sadman
Wasi Sadman

Reputation: 1482

RecyclerView is not populating items whereas Adapter and ArrayList already has data

recycerViewOrderNewItem and offlineOrderProductListProductList are two recyclerviews and those were initialized in onCreate() method.

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

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

The below is where I am retrieving my data as List<>

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

and I am setting adapter like this for both of them...

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

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

for offlineOrderProductListProductList recyclerview is working but for recycerViewOrderNewItem recyclerview is not working

I have debugged the code. ArrayList contains data.

Below is my adapter code...

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

}

please help me out with this problem...

Upvotes: 2

Views: 534

Answers (1)

Md Imran Hossain
Md Imran Hossain

Reputation: 44

I think you should initialize your adapter and recyclerview clearly.

allItemsOfOrder can be global like this

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

Take the code below as an example:

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

Then you should add orders to your list

allItemsOfOrder can be global like this

allItemsOfOrder.add(/*Something*/);

Then you should notify your adapter like below...

mAdapter.notifyDataSetChanged();

you can use this link as a reference.

Upvotes: 2

Related Questions