Rana Umerfarooq
Rana Umerfarooq

Reputation: 71

how to pass Array list <Object> to another activity

I pass Array list to intent but give an error . so please tell me what is wrong. This is my code

For Sending

addToCartList=new ArrayList<>();
Intent intent=new Intent(ShopingCart.this,SelectedProductFromShopingCartShow.class);
intent.putExtra("selectedList", (Serializable) addToCartList);
startActivity(intent);

And its my Receiver code

public class SelectedProductFromShopingCartShow extends AppCompatActivity{

    ArrayList<ShowProducts> arrayList=new ArrayList<>();
    String condition="SelectedItemsFromShoppingCart";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_selected_product_from_shoping_cart_show);

        arrayList= (ArrayList<ShowProducts>) getIntent().getSerializableExtra("selectedList");

    }
}

Here's my error

07-25 20:15:54.280 16503-16503/com.sizdom.sizdomstockmanager E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.RuntimeException: Parcel: unable to marshal value ShowProducts{product_name='Almost new', product_photo='http://192.168.1.39:81/sizdom/sizdomstock/product_images/1-Almost-new-17-07-11-10-27-21.jpg', product_sizes=0, product_created_date='null'}
at android.os.Parcel.writeValue(Parcel.java:1235)
at android.os.Parcel.writeList(Parcel.java:622)
at android.os.Parcel.writeValue(Parcel.java:1195)
at android.os.Parcel.writeMapInternal(Parcel.java:591)
at android.os.Bundle.writeToParcel(Bundle.java:1627)
at android.os.Parcel.writeBundle(Parcel.java:605)
at android.content.Intent.writeToParcel(Intent.java:6660)
at android.app.ActivityManagerProxy.startActivity(ActivityManagerNative.java:1865)
at android.app.Instrumentation.execStartActivity(Instrumentation.java:1424)
at android.app.Activity.startActivityForResult(Activity.java:3370)
at android.support.v4.app.BaseFragmentActivityJB.startActivityForResult(BaseFragmentActivityJB.java:50)
at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:79)
at android.app.Activity.startActivityForResult(Activity.java:3331)
at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:859)
at android.app.Activity.startActivity(Activity.java:3566)
at android.app.Activity.startActivity(Activity.java:3534)
at com.sizdom.sizdomstockmanager.ShopingCart$1.onClick(ShopingCart.java:66)
at android.view.View.performClick(View.java:4204)
at android.view.View$PerformClick.run(View.java:17355)
at android.os.Handler.handleCallback(Handler.java:725)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5069)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
at dalvik.system.NativeStart.main(Native Method)
07-25 20:20:54.325 700-910/system_process E/InputDispatcher: channel '536cf278 com.sizdom.sizdomstockmanager/com.sizdom.sizdomstockmanager.SplashScrean (server)' ~ Channel is unrecoverably broken and will be disposed!
07-25 20:20:54.325 700-910/system_process E/InputDispatcher: channel '535f9848 com.sizdom.sizdomstockmanager/com.sizdom.sizdomstockmanager.MainActivity (server)' ~ Channel is unrecoverably broken and will be disposed!
07-25 20:20:54.381 21855-21855/? E/Trace: error opening trace file: No such file or directory (2)
07-25 20:20:54.557 21855-21855/? E/dalvikvm: Could not find class 'android.graphics.drawable.RippleDrawable', referenced from method android.support.v7.widget.AppCompatImageHelper.hasOverlappingRendering

ShowProducts class

public class ShowProducts {
    String product_name;
    String product_photo;
    int product_sizes;
    String product_created_date;
    int size_id;
    String size_name;
    int size_price;
    int size_cost;
    int size_quantity;
    int product_id;

    public int getProduct_id() {
        return product_id;
    }

    public void setProduct_id(int product_id) {
        this.product_id = product_id;
    }

    public String getProduct_name() {
        return product_name;
    }

    public void setProduct_name(String product_name) {
        this.product_name = product_name;
    }

    public String getProduct_photo() {
        return product_photo;
    }

    public void setProduct_photo(String product_photo) {
        this.product_photo = product_photo;
    }

    public int getProduct_sizes() {
        return product_sizes;
    }

    public void setProduct_sizes(int product_sizes) {
        this.product_sizes = product_sizes;
    }

    public String getProduct_created_date() {
        return product_created_date;
    }

    public void setProduct_created_date(String product_created_date) {
        this.product_created_date = product_created_date;
    }

    public int getSize_id() {
        return size_id;
    }

    public void setSize_id(int size_id) {
        this.size_id = size_id;
    }

    public String getSize_name() {
        return size_name;
    }

    public void setSize_name(String size_name) {
        this.size_name = size_name;
    }

    public int getSize_price() {
        return size_price;
    }

    public void setSize_price(int size_price) {
        this.size_price = size_price;
    }

    public int getSize_cost() {
        return size_cost;
    }

    public void setSize_cost(int size_cost) {
        this.size_cost = size_cost;
    }

    public int getSize_quantity() {
        return size_quantity;
    }

    public void setSize_quantity(int size_quantity) {
        this.size_quantity = size_quantity;
    }

}

Upvotes: 1

Views: 6868

Answers (6)

Prasad PH
Prasad PH

Reputation: 105

First of all, serializing the items and passing it as the serializable list in Android is not recomended.

Android provides a secure and fast way to do this. We can use "Parcelable" interface. Use something like the below "Parcelable pojo" to move your list items from one activity to another.

public class ShowProducts implements Parcelable {

// THis class implents pracellable but does not create a CREATOR field
String product_name;
String product_photo;
int product_sizes;
String product_created_date;
int size_id;
String size_name;
int size_price;
int size_cost;
int size_quantity;
int product_id;

protected ShowProducts(Parcel in) {
    product_name = in.readString();
    product_photo = in.readString();
    product_sizes = in.readInt();
    product_created_date = in.readString();
    size_id = in.readInt();
    size_name = in.readString();
    size_price = in.readInt();
    size_cost = in.readInt();
    size_quantity = in.readInt();
    product_id = in.readInt();
}

public static final Creator<ShowProducts> CREATOR = new Creator<ShowProducts>() {
    @Override
    public ShowProducts createFromParcel(Parcel in) {
        return new ShowProducts(in);
    }

    @Override
    public ShowProducts[] newArray(int size) {
        return new ShowProducts[size];
    }
};

public int getProduct_id() {
    return product_id;
}

public void setProduct_id(int product_id) {
    this.product_id = product_id;
}

public String getProduct_name() {
    return product_name;
}

public void setProduct_name(String product_name) {
    this.product_name = product_name;
}

public String getProduct_photo() {
    return product_photo;
}

public void setProduct_photo(String product_photo) {
    this.product_photo = product_photo;
}

public int getProduct_sizes() {
    return product_sizes;
}

public void setProduct_sizes(int product_sizes) {
    this.product_sizes = product_sizes;
}

public String getProduct_created_date() {
    return product_created_date;
}

public void setProduct_created_date(String product_created_date) {
    this.product_created_date = product_created_date;
}

public int getSize_id() {
    return size_id;
}

public void setSize_id(int size_id) {
    this.size_id = size_id;
}

public String getSize_name() {
    return size_name;
}

public void setSize_name(String size_name) {
    this.size_name = size_name;
}

public int getSize_price() {
    return size_price;
}

public void setSize_price(int size_price) {
    this.size_price = size_price;
}

public int getSize_cost() {
    return size_cost;
}

public void setSize_cost(int size_cost) {
    this.size_cost = size_cost;
}

public int getSize_quantity() {
    return size_quantity;
}

public void setSize_quantity(int size_quantity) {
    this.size_quantity = size_quantity;
}

@Override
public int describeContents() {
    return 0;
}

@Override
public void writeToParcel(Parcel parcel, int i) {
    parcel.writeString(product_name);
    parcel.writeString(product_photo);
    parcel.writeInt(product_sizes);
    parcel.writeString(product_created_date);
    parcel.writeInt(size_id);
    parcel.writeString(size_name);
    parcel.writeInt(size_price);
    parcel.writeInt(size_cost);
    parcel.writeInt(size_quantity);
    parcel.writeInt(product_id);
}

}

You can use below code to move your array list to another activity.

addToCartList=new ArrayList<>();
    Intent intent=new Intent(ShopingCart.this,SelectedProductFromShopingCartShow.class);
    intent.putParcelableArrayListExtra("selectedList",addToArrayList);
    startActivity(intent);

Use below code to receive it in another activity.

getIntent().getParcelableArrayListExtra("selectedList");

For Android Studio the shortcut for implementing Parcelable interface is ALT+ENTER. Just implement the Parcelable interface with implementing the Java class. Android studio will intimate you with red error to implement and create the CREATOR. Android Studio will generate the beautiful code for you. That's it. Enjoy.

Upvotes: 1

Mahesh Gawhane
Mahesh Gawhane

Reputation: 338

The following code works for me:

 Intent intent = new Intent(this, editList.class);
 intent.putStringArrayListExtra("list", list);
 startActivity(intent);

You can get the result using:

ArrayList<String>() array_list = new ArrayList<String>();

array_list = getIntent().getStringArrayListExtra("list");

Upvotes: 1

PRATEEK BHARDWAJ
PRATEEK BHARDWAJ

Reputation: 2442

Please write ShowProducts implements Serializable in your model class.
This will resolve the issue.

Upvotes: 5

Sachin Chauhan
Sachin Chauhan

Reputation: 112

Please try this ...

addToCartList=new ArrayList<>();
 Intent intent=new 
 Intent(ShopingCart.this,SelectedProductFromShopingCartShow.class);
 intent.putStringArrayListExtra("selectedList", addToCartList);
 startActivity(intent);

In Your receiving Activity...

public class SelectedProductFromShopingCartShow extends AppCompatActivity{
ArrayList<ShowProducts> arrayList=new ArrayList<>();
String condition="SelectedItemsFromShoppingCart";

@Override
protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);        
  setContentView(R.layout.activity_selected_product_from_shoping_cart_show);
  arrayList= (ArrayList<ShowProducts>) 
  getIntent().getStringArrayListExtra("selectedList");
}}

Upvotes: 0

Bubu
Bubu

Reputation: 1543

Your ShowProducts object must implements Parcelable. You can easily make your Object parcelable with an online tool like : http://www.parcelabler.com/

This post can help you : How can I make my custom objects Parcelable?

Upvotes: 0

Prasad PH
Prasad PH

Reputation: 105

There is no built in method to pass array list of objects from one activity to another activity via intents. You need to implement Parcelable interface. Checkout developer console for the documentation.

https://developer.android.com/reference/android/os/Parcelable.html

Upvotes: 1

Related Questions