2017-02-02 27 views
0

我有n個QR碼,包含整數的價格。我想掃描每個二維碼並將其價格添加到總金額中。這裏是MainActivity.java 例如。如果我掃描包含價格40的qr代碼,則40將顯示在TextView2中。如何添加n個qr碼並在每次掃描qrcode時更新總量?我如何掃描多個QR碼並獲得添加?

package com.mycompany.smartshoppingcart; 

import android.net.Uri; 
import android.os.Bundle; 
import android.app.Activity; 
import android.app.AlertDialog; 
import android.content.ActivityNotFoundException; 
import android.content.DialogInterface; 
import android.content.DialogInterface.OnClickListener; 
import android.content.Intent; 
import android.view.Menu; 
import android.view.View; 
import android.widget.Button; 
import android.widget.TextView; 

public class MainActivity extends Activity { 

private static final int ACTIVITY_RESULT_QR_DRDROID = 0; 

TextView report; 
Button scan; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    report = (TextView) findViewById(R.id.textView2); 
    scan = (Button) findViewById(R.id.button1); 

    scan.setOnClickListener(new View.OnClickListener() { 

     @Override 
     public void onClick(View arg0) { 
      // TODO Auto-generated method stub 

      Intent i = new Intent("la.droid.qr.scan"); 

      try { 

       startActivityForResult(i, ACTIVITY_RESULT_QR_DRDROID); 
      } 
      catch (ActivityNotFoundException activity) { 

       MainActivity.qrDroidRequired(MainActivity.this); 
      } 
     } 
    }); 
} 

@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    // TODO Auto-generated method stub 
    super.onActivityResult(requestCode, resultCode, data); 

    if(ACTIVITY_RESULT_QR_DRDROID == requestCode 
      && data != null && data.getExtras() != null) { 

     String result = data.getExtras().getString("la.droid.qr.result"); 



     report.setText(result); 
     report.setVisibility(View.VISIBLE); 
    } 
} 

/* 
* 
* If we don't have QRDroid Application in our Device, 
* It will call below method (qrDroidRequired) 
* 
*/ 

protected static void qrDroidRequired(final MainActivity activity) { 
    // TODO Auto-generated method stub 

    AlertDialog.Builder AlertBox = new AlertDialog.Builder(activity); 

    AlertBox.setMessage("QRDroid Missing"); 

    AlertBox.setPositiveButton("Direct Download", new OnClickListener() { 

     @Override 
     public void onClick(DialogInterface arg0, int arg1) { 
      // TODO Auto-generated method stub 

      activity.startActivity(new Intent(Intent.ACTION_VIEW, 
        Uri.parse("http://droid.la/apk/qr/"))); 
     } 
    }); 

    AlertBox.setNeutralButton("From Market", new OnClickListener() { 

     @Override 
     public void onClick(DialogInterface dialog, int which) { 
      // TODO Auto-generated method stub 

      activity.startActivity(new Intent(Intent.ACTION_VIEW, 
        Uri.parse("http://market.android.com/details?id=la.droid.qr"))); 
     } 
    }); 

    AlertBox.setNegativeButton("Cancel", new OnClickListener() { 

     @Override 
     public void onClick(DialogInterface dialog, int which) { 
      // TODO Auto-generated method stub 

      dialog.cancel(); 
     } 
    }); 

    AlertBox.create().show(); 
} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.menu_main, menu); 


    return true; 
} 

}

回答

0

你應該只存儲在類的全局變量此值。

在類的頂部添加int totalCost = 0;(以下Button scan;線)

然後在onActivityResult,一旦你得到的結果從QR碼回你應該

通過int cost = Integer.parseInt(result);

施放此變量爲一個整數一旦你有成本的整數值只需將其加到總成本中

totalCost += cost;

然後更新文本標籤

report.setText("" + totalCost);