如何将Array list <object>传递给另一个activity

时间:2017-07-25 12:30:58

标签: android object android-intent arraylist parameter-passing

我将Array列表传递给intent,但是会出错。所以请告诉我有什么问题。 这是我的代码

发送

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

以及我的Receiver代码

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");

    }
}

这是我的错误

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类

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;
    }

}

6 个答案:

答案 0 :(得分:2)

请在您的模型类中写下ShowProducts implements Serializable 这将解决问题。

答案 1 :(得分:1)

没有内置方法可以通过意图将对象的数组列表从一个活动传递到另一个活动。您需要实现Parcelable接口。 Checkout开发人员控制台以获取文档。

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

答案 2 :(得分:1)

首先,不建议对项目进行序列化并将其作为Android中的可序列化列表传递。

Android提供了一种安全而又快速的方法。我们可以使用“可拆分”界面。使用类似下面的“ Parcelable pojo”之类的东西,将列表项从一项活动移至另一项活动。

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);
}

}

您可以使用以下代码将阵列列表移动到另一个活动。

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

使用下面的代码在另一个活动中接收它。

getIntent().getParcelableArrayListExtra("selectedList");
  

对于Android Studio,实现Parcelable界面的快捷方式是ALT + ENTER。   只需通过实现Java类来实现Parcelable接口即可。   Android Studio将以红色错误提示您实现和创建CREATOR。 Android Studio将为您生成漂亮的代码。   而已。请享用。

答案 3 :(得分:0)

请试试这个......

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

在您的接收活动中......

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");
}}

答案 4 :(得分:0)

您的ShowProducts对象必须实现Parcelable。 您可以使用在线工具轻松制作您的对象,例如:http://www.parcelabler.com/

这篇文章可以帮助您:How can I make my custom objects Parcelable?

答案 5 :(得分:0)

以下代码适用于我:

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

您可以使用以下方式获得结果:

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

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