How to save multiple values to a single variable through an activity.

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.

Answers


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.


Need Your Help

NSUserDefaults NSMutable Array Not Saving Between Sessions

iphone objective-c synchronization nsmutablearray nsuserdefaults

I have an NSMutableArray stored in NSUserDefaults. The mutable array is properly edited and the results of the edit can be viewed with:

Does Trie Node store the character value?

algorithm data-structures trie

I am learning Trie and get a bit confused from the description in wikipedia:

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.