如何将Parcelable数组数据发送回父活动android?

时间:2014-07-16 08:33:31

标签: java android arrays android-intent

我正在关注示例Here link

用于将子列表数组从子活动发送到父活动但是 检索通过意图发送的数组列表

时在父活动中获取错误

这是代码:

    ArrayList<StockDetails> stockList = new ArrayList<StockDetails>();

   setting array list data in some function after that 

    Intent intent = new Intent();
    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    //intent.putExtra("stkList",bndle);
    intent.putParcelableArrayListExtra("stkList",stockList );

在父母活动中

@SuppressWarnings("unchecked")
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {     
    if (resultCode == RESULT_OK ) {    
        if (data.hasExtra("stkList")) {             
            //Bundle wrapper = data.getBundleExtra("stkList");                          
                ArrayList<StockDetails> stockkList = data.getParcelableArrayListExtra("stkList");
                System.out.println("stockkList1::"+stockkList.size());                  
                System.out.println("stockkList2::"+stockkList.get(0).getStkQty());---here the error             

        }
    }
}

股票详情类

package com.eanda.mobisale.model;

import java.io.Serializable;

import android.os.Parcel;
import android.os.Parcelable;

public class StockDetails implements Comparable<StockDetails>,Parcelable,Serializable {
    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    // protected variables
    protected int eatPrdSubCatId;
    protected String eatMstStkGrpId;
    protected String stkGrpNm;
    protected String eatMstStkItemId;
    protected String stkItemNm;
    protected int stkQty;

    @Override
    public int compareTo(StockDetails another) {
        if (this.stkItemNm.equals(another.stkItemNm)) {
            return ((Integer) this.eatPrdSubCatId)
                    .compareTo(another.eatPrdSubCatId);
        } else {
            return this.stkItemNm.compareTo(another.stkItemNm);
        }
    }

    @Override
    public String toString() {
        return this.stkItemNm;
    }



    /**
     * @return the eatPrdSubCatId
     */
    public int getEatPrdSubCatId() {
        return eatPrdSubCatId;
    }

    /**
     * @param eatPrdSubCatId
     *            the eatPrdSubCatId to set
     */
    public void setEatPrdSubCatId(int eatPrdSubCatId) {
        this.eatPrdSubCatId = eatPrdSubCatId;
    }

    /**
     * @return the eatMstStkGrpId
     */
    public String getEatMstStkGrpId() {
        return eatMstStkGrpId;
    }

    /**
     * @param eatMstStkGrpId
     *            the eatMstStkGrpId to set
     */
    public void setEatMstStkGrpId(String eatMstStkGrpId) {
        this.eatMstStkGrpId = eatMstStkGrpId;
    }

    /**
     * @return the stkGrpNm
     */
    public String getStkGrpNm() {
        return stkGrpNm;
    }

    /**
     * @param stkGrpNm
     *            the stkGrpNm to set
     */
    public void setStkGrpNm(String stkGrpNm) {
        this.stkGrpNm = stkGrpNm;
    }

    /**
     * @return the eatMstStkItemId
     */
    public String getEatMstStkItemId() {
        return eatMstStkItemId;
    }

    /**
     * @param eatMstStkItemId
     *            the eatMstStkItemId to set
     */
    public void setEatMstStkItemId(String eatMstStkItemId) {
        this.eatMstStkItemId = eatMstStkItemId;
    }

    /**
     * @return the stkItemNm
     */
    public String getStkItemNm() {
        return stkItemNm;
    }

    /**
     * @param stkItemNm
     *            the stkItemNm to set
     */
    public void setStkItemNm(String stkItemNm) {
        this.stkItemNm = stkItemNm;
    }

    /**
     * @return the stkQty
     */
    public int getStkQty() {
        return stkQty;
    }

    /**
     * @param stkQty the stkQty to set
     */
    public void setStkQty(int stkQty) {
        this.stkQty = stkQty;
    }

    @Override
    public int describeContents() {
        return this.hashCode();

    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeInt(stkQty);
        dest.writeString(stkItemNm);        
    }


    public static final Parcelable.Creator<StockDetails> CREATOR
    = new Parcelable.Creator<StockDetails>() {
        public StockDetails createFromParcel(Parcel in) {
            return new StockDetails(in);
        }

        public StockDetails[] newArray(int size) {
            return new StockDetails[size];
        }
    };

     private StockDetails(Parcel in) {
         int qty = in.readInt();
         String  nm = in.readString();
         new StockDetails(qty, nm);
         //stockDetails = new StockDetails(qty, nm);
     }

    public StockDetails(int qty, String nm) {
        this.stkQty = qty;
        this.stkItemNm = nm;
    }

    public StockDetails() {
        // TODO Auto-generated constructor stub
    }
}

获取记录器stockkList1 :: 1和stockkList2 :: 0这是不正确的

1 个答案:

答案 0 :(得分:1)

您没有正确实施,Constructor with Parcel as input应该是:

private StockDetails(Parcel in) {
     this.stkQty = in.readInt();
     this.stkItemNm = in.readString();
 }

您也不需要在使用Serializable时实施Parcelable

希望这有帮助。