Passing an ArrayList of Objects to the new Activity

Viewed 61004

I have an ArrayList of objects. ie ArrayList<ObjectName>.

I want to pass this to a new Activity. I tried to use putParcelableArrayList but it has issues with the object. I removed the <ObjectName> part from the creating of the variable and the method works but then I get eclipse complaining about unsafe stuff.

How do I pass this ArrayList<ObjectName> to a new Activity

Thanks for your time

EDIT I tried this :

ArrayList<ObjectName> arraylist = new Arraylist<ObjectName>();
Bundle bundle = new Bundle();
bundle.putParcelableArrayList("arraylist", arraylist);

I get the following Error:

The method `putParcelableArrayList(String, ArrayList<? extends Parcelable>)` in the type `Bundle` is not applicable for the arguments `(String, ArrayList<ObjectName>)`

EDIT2 Object Example Code. Do I need to changed this for Parcelable to work?

public class ObjectName {
    private int value1;
    private int value2;
    private int value3;

    public ObjectName (int pValue1, int pValue2, int Value3) {
        value1 = pValue1;
        value2 = pValue2;
        value3 = pValue3;
    }

    // Get Statements for each value below
    public int getValue1() {
        return value1;
    } 
    // etc
10 Answers

Very Easy way, try the following:

bundle.putSerializable("lstContact", (Serializable) lstObject);
lstObj = (List<Contacts>) bundle.getSerializable("lstContact");

and also you will need to implement your Contact class from Serializable interface. ex :

public class Contact implements Serializable{
   ...
   ...
   ...
}

You can pass your arraylist using Serializable interface. (Serialization is the process of converting an object into a stream of bytes in order to store the object or transmit it to memory, a database, or a file.)

public class ObjectName implements Serializable{
    private int Value1;
    private int Value2;
    private int Value3;

    public ObjectName (int pValue1, int pValue2, int Value3) {
        Value1 = pValue1;
        Value2 = pValue2;
        Value3 = pValue3;
    }

    // Get Statements for each value below
    public int getValue1() {
        return Value1;
    } 
    // etc
}

Now in your current activity

// here is the arrayList of yours
ArrayList<ObjectName> objNamesList = new ArrayList<>();
objNamesList.add(new ObjectName(1,1,1)); 
objNamesList.add(new ObjectName(2,2,2)); 
//intialize bundle instance
Bundle b = new Bundle();
//adding data into the bundle 
//key => objNames referece for the arrayList
//value => objNamesList , name of the arrayList 
//cast the arraylist into Serializable 
b.putSerializable("objNames", (Serializable) objNamesList);
//init the intent
Intent i = new Intent(CurrentActivity.this, NextActivity.class);
i.putExtras(b);
startActivity(i);

at next activity you will need to do as follows to get the arraylist

public class NextActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_next_activity);
        // get the bundle
        Bundle b = getIntent().getExtras();
        ArrayList<ObjectName> q = (ArrayList<ObjectName>) b.getSerializable("objNames");
    }
}

1) Make sure the object you want to parcel has correctly implemented Parcelable

2) Use this argument in putParcelableArrayList: new ArrayList<>(list)

Example:

    protected void onSaveInstanceState(Bundle outstate) {
        super.onSaveInstanceState(outstate);
        outstate.putParcelableArrayList("myObjects", new ArrayList<>(myListOfObjects));
    }

what helped me was: setting all values while writing to the parcel. In case any value is NULL, I am setting deafult value for that particular property. Null values were getting set for few parameters, and thats why this error was being thrown.

Related