3

I am creating an application in which I scan a number of bar codes and keep extracting their values and clubbing them in a single place. As simple as this sounds I have been unable to create either an array in which I keep storing new values or a string where I keep concatenating them.

Please comment in case someone needs more code or explanation, I understand the question might not be very rich in either.

EDIT :

 public class example 
 {

    String val;
      @Override
        public void onCreate(Bundle savedInstanceState)
        {
          super.onCreate(savedInstanceState);
          try
          {

              String list_id=ToolList3.ID; 
               String list_qty=ToolScanDet3.qty;

              // val is returned from the barcode scanning app.

              val=val+issue_id+"~"+issue_qty+";";
              Log.d("Tools issued till yet...", val);

              /* Club all the tool IDs together and fire a single query to issue 
                                 them all against one name. */

              Intent i=new Intent(Issue.this,Issue1.class);
              startActivity(i); 

              //Issue1 again returns a pair of id and qty which needs to be saved along with the previous set of values.
          }

I am basically having trouble trying to save the returned set of values along with the previous ones, the new ones that are returned wipe out the previous values. I tried putting them in an array too but that requires a counter which again defeats the purpose because the counter will be initialized to zero and start over again.

Garima Tiwari
  • 1,490
  • 6
  • 28
  • 46
  • seems like you want them so save in a SQLiteDB or in a SharedPreference --> check out this: http://stackoverflow.com/questions/15948107/how-to-hold-the-selected-value-in-the-radio-button/15948303#15948303 - just for the concept, not the similarity of the question. – OschtärEi May 03 '13 at 06:49
  • @tenhouse, shared preference does not suit my logic, please check the update. – Garima Tiwari May 03 '13 at 08:30

2 Answers2

2

Unless the number of elements is known and constant, it is preferred to use ArrayList instead of array. In the case when you want to keep the data when the activity is destroyed caused by orientation change, you can save them in onSavedInstanceState :

@Override
protected void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    outState.putString("temp", tempString);
}

Then retrieve it back in onCreate:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);     

    if(savedInstanceState != null) {
        your_arraylist = savedInstanceState.getString("temp");
    }

EDIT: According to what you want, the Scan activity should not initialize any string. It should obtain the string value which is passed to it by the main instead:

    public class ScanActivity extends Activity {

    String tempString;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if(savedInstanceState == null) {
        tempString = getIntent().getStringExtra("temp");
    } else {
        // orientation change
        tempString = saveInstanceState.getString("temp");
    }
}

Once you have finished the scan, do

Intent output = new Intent();
output.putExtra("temp", tempString);
setResult(RESULT_OK, output);
finish();

to send back the string to your Main activity.

Neoh
  • 15,906
  • 14
  • 66
  • 78
  • Can you please check the update and refine your answer accordingly? This is definitely along the string of solution im looking for. Thankyou! – Garima Tiwari May 03 '13 at 08:31
  • It appears you only need to store one string instead of an array of strings. Is that all? – Neoh May 03 '13 at 08:35
  • Like I mentioned, each time I fire an activity it will return a pair a values, and I need them to keep getting added to an array or variable until I fire another activity and pass it the entire collection. – Garima Tiwari May 03 '13 at 08:39
  • If some value pre-exists before you start an activity, I suggest you pass the data as intent to the new activity, then send back the data to the main activity (see this:http://stackoverflow.com/questions/920306/sending-data-back-to-the-main-activity-in-android) – Neoh May 03 '13 at 08:52
  • Each time I scan something, it will generate some value. I want to be able to scan multiple things and keep storing them in a single array or a string without wiping out the value of the previous scan when I append the new value. The issue is not with the intent, its with storing all the values successfully in the first place! – Garima Tiwari May 03 '13 at 08:58
  • Let's say you want to start a Scan activity from MainActivity. You create a string variable total_string in main, and send a temp_string to Scan activity to return the result. Then you concatenate the total = total_string + temp_string after the scan activity finishes. You repeat the steps for another scan. Is this what you want, or am I missing something here? – Neoh May 03 '13 at 09:03
  • Thats EXACTLY what I want, only its not that simple. I will have to initialize this temp string you're talking about somewhere right? Each time I call an activity it will get re-initialized and my previous values will be lost. – Garima Tiwari May 03 '13 at 09:28
  • 1
    Please see new edit. The link I provided previously will be helpful. – Neoh May 03 '13 at 09:43
  • @ Neoh : +1 for the instant reply and the help! I didn't use the solution you suggested but thanks for your time anyway! – Garima Tiwari May 11 '13 at 03:53
0

I could not find any solution that was feasible to my situation and thats why I had to create a local database using SQL Lite. Pushing values to the database each time needed and then retrieving the values after my work flow was over.

Comment in case anyone needs help with the creation of a local database using SQL Lite. Happy to help :)

Garima Tiwari
  • 1,490
  • 6
  • 28
  • 46