【问题标题】:How to send Parcable array data back to Parent activity android?如何将 Parcelable 数组数据发送回父活动 android?
【发布时间】:2014-07-16 08:33:30
【问题描述】:

我正在关注示例Here link

用于将数据 lin List 数组从子活动发送到父活动,但是 检索按意图发送的数组列表时父活动出错

代码如下:

    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

【问题讨论】:

  • 以同样的方式。使用对 startActivityForResult/onActivityResult

标签: java android arrays android-intent


【解决方案1】:

你没有正确实现,你的Constructor with Parcel as input应该是:

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

你也不需要像使用Parcelable那样实现Serializable。

希望这会有所帮助。

【讨论】:

  • 这就是你所拥有的private StockDetails(Parcel in) { int qty = in.readInt(); String nm = in.readString(); new StockDetails(qty, nm); //stockDetails = new StockDetails(qty, nm); } 它是不同的。
  • 很高兴能帮到你..玩得开心编码:)
猜你喜欢
  • 2014-09-05
  • 2023-04-04
  • 2012-11-15
  • 2018-04-22
  • 1970-01-01
  • 1970-01-01
  • 2016-06-19
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多