简体   繁体   中英

Passing the result from SparseArray to another activity

I am trying to scan a QR code and pass the value from the scanning result to another activity. I get the result in a SparseArray and extract the most latest scanned value. I am unable to retrieve any string in my second activity. Can anyone tel me if the results in SparseArray are of the string format? If not, how can I receive these values in my second activity in string format?

My MainActivity

@Override
public void receiveDetections(Detector.Detections<Barcode> detections) {
    final SparseArray<Barcode> barcodes = detections.getDetectedItems();
    if (barcodes.size() != 0) {
        Intent intent = new Intent(MainActivity.this, SecondActivity.class);
        intent.putExtra("barcode",barcodes.valueAt(0));
        startActivity(intent);
        finish();
    }
}

My Receiving Activity

Intent intent = getIntent();
String barcode = intent.getStringExtra("barcode");

barcodes.valueAt(0) is returning a Barcode which is implementing Parcelable . In your receiving Activity you should do:

Barcode barcode = (Barcode) intent.getParcelableExtra("barcode");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM