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

时间:2017-01-12 15:19:57

标签: java android android-layout if-statement barcode

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

以下是代码:

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

答案 0 :(得分:0)

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

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

所以至少你可以看到result.getContents()包含的内容,并且可以采取相应的行动。您当前的解决方案也不具备可扩展性 - 我会研究使用switch语句或使用SQLite查询产品数据库。