Reputation: 233
In my application, i am having scan button. On its click i am opening another application "BarcodeScanner" which scans the barcode and returns the result to my application in onActivityResult where i am adding the result on ArrayList. If i again click scan button and again open BarcodeReader application to scan the next barcode, my previous result on the arraylist is getting cleared. So everytime i get the result of BarcodeScanner, only recent result is present on ArrayList. How to maintain all reslutls so that i can show all scanned barcodes in a list? Please help..
Here is my code
public void onClick(View v){
//check for scan button
if(v.getId()==R.id.scan_barcode){
//instantiate ZXing integration class
IntentIntegrator scanIntegrator = new IntentIntegrator(this);
//start scanning
scanIntegrator.initiateScan();
}
}
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
//retrieve result of scanning - instantiate ZXing object
IntentResult scanningResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, intent);
//check we have a valid result
if (scanningResult != null) {
//get content from Intent Result
String scanContent = scanningResult.getContents();
//get format name of data scanned
String scanFormat = scanningResult.getFormatName();
//Add to arraylist
barcodes.add(scanContent);
}
}
else{
//invalid scan data or scan canceled
Toast toast = Toast.makeText(getApplicationContext(),
"No scan data received!", Toast.LENGTH_SHORT);
toast.show();
}
}
Upvotes: 2
Views: 989
Reputation: 2664
1 . In your MainActivity
pass the Globally declared ArrayList through the intent
to the SecondActivity using startActivityForResult()
//Pass the ArrayList from the MainActivity
intent.putExtra("ARRAYLIST", arrayList);
startActivityForResult(intent, REQ_CODE);
// In your on Activity Result get the data
Bundle extras = intent.getExtras();
if(extras != null){
arrayList = (ArrayList) extras.get("RESULT_ARRAYLIST");
}
2 . Get the ArrayList
in the SecondActivity
, you can perform the operations on the Arraylist in the SecondActivity, When you are finished just pass the same ArrayList using the Intent by using setResult(resultCode, data)
// Get the Data in the SecondActivity
Bundle extras = getIntent().getExtras();
if(extras != null){
resultArrayList = (ArrayList) extras.get("ARRAYLIST");
}
// When you are finished
intent.putExtra("RESULT_ARRAYLIST", resultArrayList);
setResult(RESULT_OK,data);
Upvotes: 0
Reputation: 5166
Store the array list in shared preferences.
Note: Also provide a button, on which click, you can clear that list stored in shared preferences if you want. Many times, especially for testing purposes, you may want to refresh that list. So by providing an option for that, you can reset the arraylist, or still maintain data between different app executions.
Upvotes: 0
Reputation: 176
Can you show me your code? If the arraylist is a varaible on class level, it shouldn't be like that.
Here is the sample to help:
BarcodeScanner scanner = new BarcodeScanner();
ArrayList<Result> list = new ArrayList<Result>();
public void onActivityResult() {
Result value = scanner.receiveScan();
list.add(value);
}
Upvotes: 0
Reputation: 11188
1)you can use sharedprefereces to store data in arraylist
2)you can not finish your arralist activity when start barcodescanneractivity
arralist will be declare global..and u must add the value in arraylist...
i hope its useful trick to you.
Upvotes: 0