0

How do I pass an ArrayList between classes? I want to pass an ArrayList from this class/method that extends Activity...

public class OpportunityActivity extends Activity {

public void updateOpportunities(ArrayList<Opportunity> opportunities) {     
        Intent intent = new Intent(this, OppListActivity.class);
    startActivity(intent);
   }

...over to OppListActivity class that extends ListActivity where I use this opportunities ArrayList to populate my custom ListView using the elements in the arraylist opportunities:

public class OppListActivity extends ListActivity {

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

I'd like to pass opportunities arraylist between the two intact. Should I... - use a bundle (I've tried but can't work out the right syntax, and this seems like overkill/maybe there's a better way?) - make opportunities public / somehow exposed to both classes - pass opportunities as a parameter?

Specific syntax (not pseudocode) appreciated.

rihallix
  • 356
  • 1
  • 8
  • 22

5 Answers5

1

You can't create an Activity like this:

public void updateOpportunities(ArrayList<Opportunity> opportunities) {     
   OppListActivity a = new OppListActivity();
   }

You should always use an Intent. In the Intent you can add an "extra" with your ArrayList, which you will read later in your ListActivity. Note that you will probably need to make your Opportunity implement Parcelable. There's a lot of info on both of these topics both in StackOverflow and in the official docs.

dmon
  • 30,048
  • 8
  • 87
  • 96
  • 1
    Fixed the activity code. I find lots of great docs/sample code about passing strings, but nothing that has proven effective passing ArrayLists intact. I will take another look though. – rihallix Oct 29 '12 at 15:11
  • Found this very long winded example: http://stackoverflow.com/questions/5819238/help-with-passing-arraylist-and-parcelable-activity – rihallix Oct 29 '12 at 15:19
  • As you found out, sending ArrayLists is definitely possible. It's quite a pain in the butt, but it's doable of course (and the right way too). – dmon Oct 29 '12 at 15:25
  • I've found that it's better to keep most of the data retrieval local to the Activity that deals with it, if you can manage it. – dmon Oct 29 '12 at 15:26
  • Nice idea, but I seem to need to split this - I have a class that extends ListActivity where I need to use it and another class that extends something else (forget offhand) that populates the array list. – rihallix Oct 29 '12 at 15:35
  • Like I said, it's better, but sometimes you still have to pass 'em around, no biggie. – dmon Oct 29 '12 at 19:32
1

You need to use intents to start a new activity. And you can actually put serializable objects in your intent's extra data:

    public void updateOpportunities(ArrayList<Opportunity> opportunities) {     
        Intent intent = new Intent(this, OppListActivity.class);
        intent.putExtra("opportunities", opportunities);
        startActivity(intent);
    }

And in your OppListActivity activity:

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

    Intent intent = getIntent();
    ArrayList<Opportunity> opportunities = intent.getSerializableExtra("opportunities");
}

But you probably need to make sure that Opportunity class extends Serializable.

sdabet
  • 18,360
  • 11
  • 89
  • 158
1

On way is to use putExtra if you are using an Intent to trigger the next activity. It would look something like this:

ArrayList<String> test_array = new ArrayList<String>();
// do something with test_array
Intent launchNextActivity = new Intent(getApplicationContext(), nextActivity.class);
launchNextActivity.putExtra("array", test_array);
startActivity(launchNextActivity);

And then in the activity you launch, you could use the following to get it:

Intent sender = getIntent();
ArrayList<String> passedInArray = new ArrayList<String>();
passedInArray = sender.getStringArrayExtra("array");
burmat
  • 2,548
  • 1
  • 23
  • 28
0

usually you add an extra bundle when starting a new intent when switching to some other activity. e.g

ArrayList<String> theArrayList = new ArrayList<String>();
Intent i = new Intent(this,TheNewActivity.class);
i.putStringArrayListExtra("list",theArrayList);
startActivity(i);
finish();
dunn less
  • 623
  • 10
  • 26
0

hiiii..... You can use Bean,with the one ArrayList

e.g

ArrayList<RowItem> aryListbean=new ArrayList<rowItem>(); //your arraylist
RowItem rowItem=new RowItem();  // that is your bean class

now You have to create a class RowItem,that is your Bean,which have getter/setter method

for(....)
{
   rowItem.setUserId(); // set method's example
   rowItem.setPassword() // set method's exampl

   aryListbean.add(rowItem);
}

Now you can use your data in every Activities using getmethod

e.g

getUserId();
getUserPassword(); 
Mit Bhatt
  • 1,605
  • 2
  • 13
  • 24