使用Parcelable对象传递对象数组

时间:2013-07-13 10:15:18

标签: android

我正在尝试使用Parcelable传递自定义对象的数组。我在搜索中找到了一些讨论,例如link。我按照这些示例,但无法找到对象数组的实现。我的问题是对象在新活动中变为空。 我的实现如下。

我可感知的对象

    public class showStatisticsObj implements Parcelable{
    public String dt = null;
    public int totalmiles = 0;
    public Date date = null;

    public showStatisticsObj() { ; };

    public showStatisticsObj(Parcel in) { readFromParcel(in); } 

    @Override
    public int describeContents() {
        // TODO Auto-generated method stub
        return 0;
    }
    @Override
    public void writeToParcel(Parcel arg0, int arg1) {
        // TODO Auto-generated method stub
        arg0.writeString(dt); 
        arg0.writeInt(totalmiles);      
    }


    public void readFromParcel(Parcel in) {   
        dt = in.readString(); 
        totalmiles = in.readInt(); 
    } 

    @SuppressWarnings("rawtypes")
    public static final Parcelable.Creator CREATOR = new Parcelable.Creator() { 
        public showStatisticsObj createFromParcel(Parcel in) 
        { 
            return new showStatisticsObj(in); 
        }   
        public showStatisticsObj[] newArray(int size) 
        { 
            return new showStatisticsObj[size]; 
        } 
    }; 

    }


    Parcelable[] passedArray = new Parcelable[totaldifferentDates];
    for (int i=0; i<totaldifferentDates; ++i) {
        passedArray[i] = showStatObj[i];
    }

    Intent displayintent = new Intent(this, DisplayStatistics.class);
    displayintent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    displayintent.putExtra("bundleObj", passedArray);
    startActivity(displayintent);

在新活动中,我提取为

    Bundle bundle = getIntent().getExtras();    
    showStatObjinDisplay = (showStatisticsObj) bundle.getParcelable("bundleObj");

但showStatObjinDisplay为null。 有什么问题?请帮助我。

2 个答案:

答案 0 :(得分:5)

我会像Minhtdh一样尝试改变CREATOR。

接下来你不需要代码,因为你已经有了parcelable数组(showStatisticsObj是Parcelable):

// you don't need following code
Parcelable[] passedArray = new Parcelable[totaldifferentDates];
for (int i=0; i<totaldifferentDates; ++i) {
    passedArray[i] = showStatObj[i];
}

所以你只需将showStatObj传递给intent:

Intent displayintent = new Intent(this, DisplayStatistics.class);
displayintent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
displayintent.putExtra("bundleObj", showStatObj);
startActivity(displayintent);

最后你从这样的捆绑中得到它:

Parcelable[] ps = bundle.getParcelableArrayExtra();
showStatisticsObj[] uri = new showStatisticsObj[ps.length];
System.arraycopy(ps, 0, uri, 0, ps.length);

您通过了Parcelable [],因此必须在bundle中搜索Parcelable [],然后将其转换为showStatisticsObj []。在java中,您无法将Parcelable []强制转换为showStatisticsObj []。

作为代码的建议,类名应始终以大写字母开头。所以不是showStatisticsObj,而是ShowStatisticsObj

答案 1 :(得分:-4)

最好实现Serializable接口。它比Parcelable更容易。

检查此样本:

public class Foo implements Serializable
{
    private static final long   serialVersionUID    = -459713774703414626L;
    public String               m_sName;
    public String               m_spass;
    public int                  m_iAge;
    public boolean              m_bUseAgePremissions;
}

发送

Foo MyObj=new Foo()
Bundle bData=new Bundle();
bData.putSerializable("test", MyObj);
Intent myIntent = ...
myIntent .putExtras(bData);

收到

Bundle bDataReceive = myIntent.getExtras();
Foo MyObj = (Foo)bDataReceive.getSerializable("test");