'Mobile barcode scanner to check if the order item matches the scanned barcode in Android java

I am trying to make an android application where the user works in a warehouse and picks items according to each order. They can choose an order from the recycler view in the order screen, it then goes to another screen that populates another recycler view with all the items in that order. In the recycler view on the items screen, for each item, is the item uid, barcode, description, and quantity. Next to each item there are 2 buttons on the right, a green arrow and a red x, if you click the x it sets the amount picked to 0 (there is no stock of that item in the warehouse). If you click the arrow the idea is to check if the item has a barcode, if not, it displays a dialog where the user can edit the picked amount for that item. If it does have a barcode open the scanner and scan the product that the user suspects to be the correct product. It will then perform a check to see if the item scanned matches the barcode of the item in the recycler view. If it matches then display the dialog where the user can edit the picked amount, if it does not match display toast saying "wrong product, try again."

The issue I'm having is that when I click on the arrow and the scanner opens to perform the scan and once the scan is complete nothing happens until I click on the arrow again. Only then does it either display the dialog to change picked amount if its correct, or it opens the scanner again due to it being the incorrect product. I need it to open the dialog straight after the scan if its the correct product, or display toast saying its the incorrect product.

Presumably, the reason why is only opening the dialog on correct product result after the second click is because on the first click it does the check at the same time as the scan, and therefore once the scan is completed and the user is returned to the items screen there is no dialog displaying.

A solution I tried, is to do the check outside of the arrow button onclick and in the onbindview, and once the scan is complete set the items screen to recreate(). So in essence it refreshes the screen and does the check. However this still doesn't work properly as it checks for each item in the order instead of just the item you scanned.

Below is a diagram to help visualize the scenario:

Diagram of Scenario

On arrow click:

holder.Next.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    AlertDialog.Builder builder = new AlertDialog.Builder(Order.this);
                    ViewGroup viewGroup = findViewById(android.R.id.content);
                    View dialogView = LayoutInflater.from(v.getContext()).inflate(R.layout.customdialogview, viewGroup, false);
                   
                    QuantityRequired = dialogView.findViewById(R.id.QuantityRequiredTXT);
                    QuantityPicked = dialogView.findViewById(R.id.QuantityPickedTXT);
                    DialogBarcode = dialogView.findViewById(R.id.AddBarcodeTXT);
                    Finish = dialogView.findViewById(R.id.FinishDialogBTN);
                    DialogDescription = dialogView.findViewById(R.id.AddDescriptionTXT);

                    ScannedBarcode = dialogView2.findViewById(R.id.ScanendBarcodeTXT);
                    ItemBarcode = dialogView2.findViewById(R.id.ItemBarcodeTXT);
                    Yes = dialogView2.findViewById(R.id.YesDialogBTN);
                    No = dialogView2.findViewById(R.id.CancelDialogBTN);

                    ScannedBarcode.setText(scanContent);
                    ItemBarcode.setText(holder.BarcodeTXT.getText().toString());

                    DialogBarcode.setText(holder.BarcodeTXT.getText().toString());
                    DialogDescription.setText(holder.DescriptionTXT.getText());
                    QuantityRequired.setText(holder.QuantityTXT.getText());
                    ItemUid = slipDataModel.getUID();


                    builder.setView(dialogView);
                    AlertDialog alertDialog = builder.create();    

                        if(holder.BarcodeTXT.getText().toString().equals(scanContent)){
                            alertDialog.show();
                            scan = true;
                        }

                        if(holder.BarcodeTXT.getText().toString().equals("")){

                            alertDialog.show();
                            scan = true;
                        }

                    if(!holder.BarcodeTXT.getText().toString().equals(scanContent) && !holder.BarcodeTXT.getText().toString().equals("")){
                        ScanCode();
                        barcode = holder.BarcodeTXT.getText().toString();
                        productDescription = holder.DescriptionTXT.getText().toString();
                        productQuantity = holder.QuantityTXT.getText().toString();
                        recreate();
                        scan = false;

                    } 

                    Finish.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {

                            next = true;


                            if(holder.QuantityTXT.getText().equals(slipDataModel.getQuantity() + "(0)")){
                                holder.QuantityTXT.setText("");
                            }

                            holder.QuantityTXT.setText(slipDataModel.getQuantity() + "(" + QuantityPicked.getText() + ")");

                            if(next){
                                holder.Next.setBackgroundResource(R.drawable.ic_wand_icons_01);
                            }else{
                                holder.Next.setBackgroundResource(R.drawable.ic_greenarrowfoward);
                            }


                            Log.d(TAG, "Quan: " + holder.QuantityTXT.getText());

                            Log.d(TAG, "Quantity Picked: " + QuantityPicked.getText());

                            SyncData4 Data = new SyncData4();
                            Data.execute();

                            alertDialog.dismiss();
                        }
                    });
                }
            });

The Check in the onBindView:

Checks if the item has a barcode, if not dont do the check, if it does, check if the item's barcode matches the scanContent (scanned barcode). It then binds all the values for that item to the dialog and displays it, else its the wrong product.

if (barcode != null) {
                if (barcode == scanContent) {

                    AlertDialog.Builder builder = new AlertDialog.Builder(Order.this);
                    ViewGroup viewGroup = findViewById(android.R.id.content);
                    View dialogView = LayoutInflater.from(context).inflate(R.layout.customdialogview, viewGroup, false);
                    builder.setView(dialogView);
                    AlertDialog alertDialog = builder.create();

                    QuantityRequired = dialogView.findViewById(R.id.QuantityRequiredTXT);
                    QuantityPicked = dialogView.findViewById(R.id.QuantityPickedTXT);
                    DialogBarcode = dialogView.findViewById(R.id.AddBarcodeTXT);
                    DialogDescription = dialogView.findViewById(R.id.AddDescriptionTXT);

                    DialogBarcode.setText(barcode);
                    DialogDescription.setText(productDescription);
                    QuantityRequired.setText(productQuantity);
                    //ItemUid = slipDataModel.getUID();
                    Log.d(TAG, "YES: " + barcode + scanContent);
                    alertDialog.show();

                } else {
                    Log.d(TAG, "No: " + barcode + scanContent);
                    Toast.makeText(context, "Wrong Product!", Toast.LENGTH_SHORT).show();

                }
            }

Item Screen



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source