4

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.

2
  • seems like you want them so save in a SQLiteDB or in a SharedPreference --> check out this: stackoverflow.com/questions/15948107/… - just for the concept, not the similarity of the question. Commented May 3, 2013 at 6:49
  • @tenhouse, shared preference does not suit my logic, please check the update. Commented May 3, 2013 at 8:30

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

9 Comments

Can you please check the update and refine your answer accordingly? This is definitely along the string of solution im looking for. Thankyou!
It appears you only need to store one string instead of an array of strings. Is that all?
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.
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:stackoverflow.com/questions/920306/…)
Please see new edit. The link I provided previously will be helpful.
|
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 :)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.