0
ArrayList <Car> CarList = new ArrayList<Car>();  
Car carItems= new Car(carno, cartype, date, arriveTime, carcost);   
CarList .add(carItems);

Now I want to pass carList through Intent?

3 Answers 3

1

For passing the the object:

Bundle bundle = new Bundle();
ArrayList <Car> CarList = new ArrayList<Car>();  
Car carItems= new Car(carno, cartype, date, arriveTime, carcost);   
CarList.add(carItems); 
bundle.putSerializable("carList",carList);
intent.putExtras(bundle);

For retrieving:

ArrayList <Car> CarList = getIntent().getSerializableExtra("carList");

Make sure Car is serializable:

public class Car implements Serializable {

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

3 Comments

It show error as The method putExtra(String, boolean) in the type Intent is not applicable for the arguments (String, List<Car>)
Have you made your Car class implement Parcelable
public class Car implements Parcelable and I bave implemented public int describeContents(), public void writeToParcel(Parcel dest, int flags)
0

It seems that you actually have to make Car a Parcelable.

To add it to the Intent, use putParcelableArrayListExtra(String name, ArrayList<? extends Parcelable> value)

Edit

To get the list in the other activity, do this in onCreate or onNewIntent:

Intent i = getIntent();
ArrayList<Car> cars = i.getParcelableArrayListExtra("extraKeyUsedWithPutExtra");

2 Comments

hi how to get that list in another activity?
I added info on how to get the list from another activty to the answer.
0

Make it parcleable, pretty easy.

Have a look on this posting: Arraylist in parcelable object

2 Comments

Yes but I want to pass carList through Intent.
An ArrayList full of Objects can be put into any parcel using: developer.android.com/reference/android/os/…

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.