嵌套if else语句被跳过并且什么都不做

时间:2022-07-17 07:08:34

I'm doing an app where the code will scan a barcode and check the content and if the content tally with the if else statement it will display the price. How do i solve this problem?

我正在做一个应用程序,代码将扫描条形码并检查内容,如果内容与if else语句相符,它将显示价格。我该如何解决这个问题?

Here is the code:

这是代码:

public class barcode extends AppCompatActivity {
    Button scan_btn;
    EditText Edit_current;
    TextView formatTxt, contentTxt,price;
    int x;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(activity_promotion_page);
        formatTxt = (TextView) findViewById(R.id.scanFormat);
        contentTxt = (TextView) findViewById(scanContent);
        price = (TextView)findViewById(R.id.Price);
        scan_btn = (Button) findViewById(R.id.scan_button);
        final Activity activity = this;
        scan_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                IntentIntegrator integrator = new IntentIntegrator(activity);
                integrator.setDesiredBarcodeFormats(IntentIntegrator.PRODUCT_CODE_TYPES);
                integrator.setPrompt("Scan");
                integrator.setCameraId(0);
                integrator.setBeepEnabled(false);
                integrator.setBarcodeImageEnabled(false);
                integrator.initiateScan();

            }
        });

    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);

            if(result.getContents( ) == null) {
                Log.d("MainActivity", "Cancelled scan");
                Toast.makeText(this, "Cancelled", Toast.LENGTH_LONG).show();
            } else if (result.getContents().equals(12345014)){

            price.setText("$999");
            }else if (result.getContents().equals(12345021)){

                price.setText("$91");

            }


        }}

1 个解决方案

#1


0  

With the limited information available, I believe it's because result.getContents() does not match any of the statements you're checking for. I'd add this as a final statement at the bottom:

由于可用的信息有限,我相信这是因为result.getContents()与您正在检查的任何语句都不匹配。我将此添加为底部的最终声明:

} else {
    Log.d("MainActivity", "Failed to match. Contents are " + result.getContents());
}

So at least you can see what result.getContents() contains, and can act accordingly. Your current solution is also not very scalable - I'd look into using switch statements or using SQLite to query a product database.

所以至少你可以看到result.getContents()包含什么,并且可以相应地采取行动。您当前的解决方案也不是很可扩展 - 我会考虑使用switch语句或使用SQLite查询产品数据库。

#1


0  

With the limited information available, I believe it's because result.getContents() does not match any of the statements you're checking for. I'd add this as a final statement at the bottom:

由于可用的信息有限,我相信这是因为result.getContents()与您正在检查的任何语句都不匹配。我将此添加为底部的最终声明:

} else {
    Log.d("MainActivity", "Failed to match. Contents are " + result.getContents());
}

So at least you can see what result.getContents() contains, and can act accordingly. Your current solution is also not very scalable - I'd look into using switch statements or using SQLite to query a product database.

所以至少你可以看到result.getContents()包含什么,并且可以相应地采取行动。您当前的解决方案也不是很可扩展 - 我会考虑使用switch语句或使用SQLite查询产品数据库。