It is quite simple but can save time to a novice.
As described in the subject code below allows you to pass an ArrayList between two activities.
First we must write the class that interests us. I wrote a simple class that represents a customer (fancy that!)
Using java Syntax Highlighting
- public class Customer {
- private String name;
- private String phoneNumber;
- public Customer(){
- }
- public Customer(String name, String phoneNumber){
- this.name = name;
- this.phoneNumber = phoneNumber;
- }
- public void setName(String name) {
- this.name = name;
- }
- public String getName() {
- return name;
- }
- public void setPhoneNumber(String phoneNumber) {
- this.phoneNumber = phoneNumber;
- }
- public String getPhoneNumber() {
- return phoneNumber;
- }
- }
Parsed in 0.031 seconds, using GeSHi 1.0.8.4
And then I wrote the class that represents the list of my clients. It inherits from ArrayList and implements the interface Parcelable. Believe that the code is quite clear, there are basically two methods, one that writes the contents of the ArrayList into a parcel and the other from a given Parcel creates the ArrayList. It was also declared an object of type Parcelable.Creator, which is called automatically when the list is "deserialized".
Using java Syntax Highlighting
- package simple.test;
- import java.util.ArrayList;
- import android.os.Parcel;
- import android.os.Parcelable;
- public class CustomersList extends ArrayList<Customer> implements Parcelable{
- private static final long serialVersionUID = 663585476779879096L;
- public CustomersList(){
- }
- public CustomersList(Parcel in){
- readFromParcel(in);
- }
- @SuppressWarnings("unchecked")
- public static final Parcelable.Creator CREATOR = new Parcelable.Creator() {
- public CustomersList createFromParcel(Parcel in) {
- return new CustomersList(in);
- }
- public Object[] newArray(int arg0) {
- return null;
- }
- };
- private void readFromParcel(Parcel in) {
- this.clear();
- //First we have to read the list size
- int size = in.readInt();
- //Reading remember that we wrote first the Name and later the Phone Number.
- //Order is fundamental
- for (int i = 0; i < size; i++) {
- Customer c = new Customer();
- c.setName(in.readString());
- c.setPhoneNumber(in.readString());
- this.add(c);
- }
- }
- public int describeContents() {
- return 0;
- }
- public void writeToParcel(Parcel dest, int flags) {
- int size = this.size();
- //We have to write the list size, we need him recreating the list
- dest.writeInt(size);
- //We decided arbitrarily to write first the Name and later the Phone Number.
- for (int i = 0; i < size; i++) {
- Customer c = this.get(i);
- dest.writeString(c.getName());
- dest.writeString(c.getPhoneNumber());
- }
- }
- }
Parsed in 0.037 seconds, using GeSHi 1.0.8.4
and then i show you how to write the CallerActivity:
Using java Syntax Highlighting
- package simple.test;
- import android.app.Activity;
- import android.content.Intent;
- import android.os.Bundle;
- public class CallerActivity extends Activity {
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.caller);
- //Create and populate the list
- CustomersList customers = new CustomersList();
- Customer c1 = new Customer("First Customer", "123456");
- Customer c2 = new Customer("Second Custimer", "7891011");
- customers.add(c1);
- customers.add(c2);
- Bundle b = new Bundle();
- b.putParcelable("customers", customers); //Insert list in a Bundle object
- Intent i = new Intent(this, CalledActivity.class);
- i.putExtras(b); //Insert the Bundle object in the Intent' Extras
- startActivity(i); //Start Activity
- }
- }
Parsed in 0.039 seconds, using GeSHi 1.0.8.4
and the called:
Using java Syntax Highlighting
- package simple.test;
- import android.app.Activity;
- import android.os.Bundle;
- import android.widget.Toast;
- public class CalledActivity extends Activity {
- private CustomersList customers;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.called);
- Bundle b = getIntent().getExtras(); //Get the intent's extras
- customers = b.getParcelable("customers"); //get our list
- Toast.makeText(this, String.valueOf(customers.size()), Toast.LENGTH_SHORT).show();
- }
- }
Parsed in 0.038 seconds, using GeSHi 1.0.8.4
hope will be useful to someone.
Regards.



