2016年1月21日 星期四

[Android] Passing ArrayList through Intent


In your object

public class Product implements Serializable {
    private int pid;
   /*... setter and getter */
}

----------------------------------------------------------------------------------------
MainActivity  - passing

 ArrayList<Product> productList = new ArrayList<Product>();

 Intent intent = new Intent( MainActivity.this , ProductItemActivity.class);
            intent.putExtra("productList", (Serializable)productList);

   startActivity(intent);


-----------------------------------------------------------------------------------------

ProductItemActivity  - receiving

private ArrayList<Product> productList;
productList =  (ArrayList<Product>) getIntent().getSerializableExtra("productList");


-----------------------------------------------------------------------------------------

To sum up, this technique works for any type of object.