Updating the arraylist which populates listview
<p>I have a listactivity whose listview is being populated from different activities.  </p>

<p>FirstActivity :  </p>

<pre><code>Intent view_order_intent = new Intent(this, thirdpage.class);
view_order_intent.putStringArrayListExtra("selectedData", (ArrayList&lt;String&gt;)selectedData);  
</code></pre>

<p>ListActivity :  </p>

<pre><code>Intent intent = getIntent();
Bundle bundle = intent.getExtras
selectedData = (ArrayList&lt;String&gt;) bundle.get("selectedData");  
</code></pre>

<p>Same goes for other three activities.<br>
<strong>Current output</strong>: I am setting an <em>int</em> in each activity and pass it through intent, which discovers the activity name and then accordingly populates the respective arraylist 
of that activity to the listview. This is same for all four activities. This way I am able to generate a listview based on the contents of single arraylist. Each time a different activity is loaded, the arraylist is loaded again and thus the listview is loaded with all new contents.</p>

<p><strong>Desired output</strong>: What I want is to populate the listview from a single arraylist which I may get by combining all the arraylists of four activityies. Also the listview should not have all new contents, but there should only be update in the contents(new array elements should be added). For which I think I need to persist the arraylist but I wonder how.<br>
Can someone help me out? I'll post code if someone wants it.  </p>

<p><strong>Update</strong>:  </p>

<p>desserts.java (one of the four activities)  </p>

<pre><code>    package a.b.layout;

    import java.util.ArrayList;
    import java.util.List;
    import android.app.ListActivity;
    import android.content.Intent;
    import android.os.Bundle;
    import android.util.SparseBooleanArray;
    import android.view.View;

import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;

public class desserts  extends ListActivity  implements  android.view.View.OnClickListener  {

    private List&lt;Item&gt; data;
    private List&lt;String&gt; selectedData;
    private ItemListAdapter adapter;
    ListView lv; 
    int iClass = 1;
    Button view_order,place_order,starters,mains,desserts,drinks;

    @Override
    public void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    setContentView(R.layout.menuslayout);
    data = new ArrayList&lt;Item&gt;(10);
    selectedData = new ArrayList&lt;String&gt;();

    data.add(new Item(10, "dessert1"));
    data.add(new Item(11, "dessert2"));
    data.add(new Item(12, "dessert3"));
    data.add(new Item(13, "dessert4"));
    data.add(new Item(14, "dessert5"));
    data.add(new Item(15, "dessert6"));
    data.add(new Item(16, "dessert7"));
    data.add(new Item(17, "dessert8"));
    data.add(new Item(18, "dessert9"));
    data.add(new Item(19, "dessert10"));
    data.add(new Item(20, "dessert11"));

    adapter = new ItemListAdapter(this, data);
    setListAdapter(adapter);
    lv = getListView();
    lv.setItemsCanFocus(false);
    lv.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
    view_order=(Button)findViewById(R.id.vieworder);
    view_order.setOnClickListener(this) ;
    starters=(Button)findViewById(R.id.starters);
    starters.setOnClickListener(this) ;
    mains=(Button)findViewById(R.id.mains);
    mains.setOnClickListener(this) ;
    drinks=(Button)findViewById(R.id.drinks);
    drinks.setOnClickListener(this) ;
    }
     //@Override
    public void onBackPressed() {
        Intent i=new Intent(this , layoutact.class);
        startActivity(i); 
        return;
    }

    @Override
    public void onClick(View v) {
        switch(v.getId())
    {
        case R.id.vieworder:
               showSelectedItems();
               Intent view_order_intent = new Intent(this, thirdpage.class);
               view_order_intent.putStringArrayListExtra("selectedData", (ArrayList&lt;String&gt;)selectedData);
               view_order_intent.putExtra("iClass", iClass);
               startActivity(view_order_intent);
               break;

        case R.id.starters:
            Intent starters_intent = new Intent(this, starters.class);
            startActivity(starters_intent);
            break;

        case R.id.mains:
            Intent mains_intent = new Intent(this, mains.class);
            startActivity(mains_intent);
            break;
        case R.id.drinks:
            Intent drinks_intent = new Intent(this, drinks.class);
            startActivity(drinks_intent);
            break;
      }
    }


    private void showSelectedItems() {
        final StringBuffer sb = new StringBuffer("Selection: ");

        // Get an array that tells us for each position whether the item is
        // checked or not
        // --
        final SparseBooleanArray checkedItems = lv
                .getCheckedItemPositions();
        if (checkedItems == null) {
            Toast.makeText(this, "No selection info available",
                    Toast.LENGTH_LONG).show();
            return;
        }

        // For each element in the status array
        // --
        boolean isFirstSelected = true;
        final int checkedItemsCount = checkedItems.size();
        for (int i = 0; i &lt; checkedItemsCount; ++i) {
            // This tells us the item position we are looking at
            // --
            final int position = checkedItems.keyAt(i);

            // This tells us the item status at the above position
            // --
            final boolean isChecked = checkedItems.valueAt(i);

            if (isChecked) {
                if (!isFirstSelected) {
                    sb.append(", ");
                }
                sb.append(data.get(position).getCaption());
                isFirstSelected = false;
                selectedData.add(data.get(position).getCaption());
            }
        }

        // Show a message with the countries that are selected
        // --
        Toast.makeText(this, sb.toString(), Toast.LENGTH_LONG).show();
    }

}
</code></pre>

<p>Other three activities are very same to desserts.java except for the contents of the list.<br>
thirdpage.java (listactivity)  </p>

<pre><code>package a.b.layout;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import android.app.ListActivity;
import android.bluetooth.BluetoothAdapter;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

public class thirdpage  extends ListActivity  implements  android.view.View.OnClickListener  {

    static final int REQUEST_ENABLE_BT = 0;
    private List&lt;Item&gt; data;
    private List&lt;Item&gt; startersData;
    private List&lt;Item&gt; mainsData;
    private List&lt;Item&gt; drinksData;
    private ViewOrderAdapter adapter;
    private ArrayList&lt;String&gt;selectedData;
    TextView txtRate;
    ImageView img;
    ListView lv; 
    int iClass;
    Button complete_order,place_order,starters,mains,desserts,drinks;
    private List&lt;Item&gt; viewData;

    /* (non-Javadoc)
     * @see android.app.Activity#onCreate(android.os.Bundle)
     */
    @SuppressWarnings("unchecked")
    @Override
    public void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    setContentView(R.layout.thirdpagelayout);
    selectedData = new ArrayList&lt;String&gt;();

    try
    {
    Intent intent = getIntent();
    Bundle bundle = intent.getExtras();
    selectedData = (ArrayList&lt;String&gt;) bundle.get("selectedData");
    iClass = bundle.getInt("iClass");
    } catch (Exception e) {
        // TODO: handle exception
        e.printStackTrace();
    }
    data = new ArrayList&lt;Item&gt;(10);
    startersData = new ArrayList&lt;Item&gt;(10);
    mainsData = new ArrayList&lt;Item&gt;(10);
    drinksData = new ArrayList&lt;Item&gt;(10);
    viewData = new ArrayList&lt;Item&gt;();

    //Desserts
    data.add(new Item(10, "dessert1"));
    data.add(new Item(11, "dessert2"));
    data.add(new Item(12, "dessert3"));
    data.add(new Item(13, "dessert4"));
    data.add(new Item(14, "dessert5"));
    data.add(new Item(15, "dessert6"));
    data.add(new Item(16, "dessert7"));
    data.add(new Item(17, "dessert8"));
    data.add(new Item(18, "dessert9"));
    data.add(new Item(19, "dessert10"));
    data.add(new Item(20, "dessert11"));

    //Starters
    startersData.add(new Item(10, "starter1"));
    startersData.add(new Item(11, "starter2"));
    startersData.add(new Item(12, "starter3"));
    startersData.add(new Item(13, "starter4"));
    startersData.add(new Item(14, "starter5"));
    startersData.add(new Item(15, "starter6"));
    startersData.add(new Item(16, "starter7"));
    startersData.add(new Item(17, "starter8"));
    startersData.add(new Item(18, "starter9"));
    startersData.add(new Item(19, "starter10"));
    startersData.add(new Item(20, "starter11"));

    //Mains
    mainsData.add(new Item(10, "mains1"));
    mainsData.add(new Item(11, "mains2"));
    mainsData.add(new Item(12, "mains3"));
    mainsData.add(new Item(13, "mains4"));
    mainsData.add(new Item(14, "mains5"));
    mainsData.add(new Item(15, "mains6"));
    mainsData.add(new Item(16, "mains7"));
    mainsData.add(new Item(17, "mains8"));
    mainsData.add(new Item(18, "mains9"));
    mainsData.add(new Item(19, "mains10"));
    mainsData.add(new Item(20, "mains11"));

    //Drinks
    drinksData.add(new Item(10, "drinks1"));
    drinksData.add(new Item(11, "drinks2"));
    drinksData.add(new Item(12, "drinks3"));
    drinksData.add(new Item(13, "drinks4"));
    drinksData.add(new Item(14, "drinks5"));
    drinksData.add(new Item(15, "drinks6"));
    drinksData.add(new Item(16, "drinks7"));
    drinksData.add(new Item(17, "drinks8"));
    drinksData.add(new Item(18, "drinks9"));
    drinksData.add(new Item(19, "drinks10"));
    drinksData.add(new Item(20, "drinks11"));

    for(int i=0;i&lt;selectedData.size();i++)
    {
        String str = selectedData.get(i);
        if(iClass == 1)
        {           
for(int idata = 0;idata&lt;data.size();idata++)
{
    if(str.equals(data.get(idata).getCaption()))
    {
         viewData.add(data.get(idata));
    }
    else
    {

    }
}
        }
else if (iClass == 2)
{
    for(int idata = 0;idata&lt;startersData.size();idata++)
    {
        if(str.equals(startersData.get(idata).getCaption()))
        {
            viewData.add(startersData.get(idata));
        }
        else
        {

        }   
}

}
else if (iClass == 3)
{
    for(int idata = 0;idata&lt;mainsData.size();idata++)
    {
        if(str.equals(mainsData.get(idata).getCaption()))
        {
            viewData.add(mainsData.get(idata));
        }
        else
        {

        }   
}
}
else if (iClass == 4)
{
    for(int idata = 0;idata&lt;drinksData.size();idata++)
    {
        if(str.equals(drinksData.get(idata).getCaption()))
        {
            viewData.add(drinksData.get(idata));
        }
        else
        {

        }   
}
}

    }
    long l = 0;
    for(int i=0;i&lt;viewData.size();i++)
    {
        l=l+viewData.get(i).getId();
    }
    TextView txtTotal = (TextView)findViewById(R.id.totalText);
    txtTotal.setText("TOTAL: $"+(String.valueOf(l)));
    adapter = new ViewOrderAdapter(this, viewData);
    setListAdapter(adapter);
    lv = getListView();
    lv.setItemsCanFocus(false);
    lv.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);


    complete_order=(Button)findViewById(R.id.completeorder);
    complete_order.setOnClickListener(this) ;
    }

    @Override
    public void onClick(View v) {
        switch(v.getId())
    {
        case R.id.completeorder:
            BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();    
            if (!mBluetoothAdapter.isEnabled()) {
                    Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
                    startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);
            }
               break;

      }
    }
}
</code></pre>
