2

あるアクティビティから別のアクティビティにクラスの配列リストを渡したいのですが、同じことを行うために、クラスはParcelableを実装しています。問題は、クラス内のいくつかのフィールドがnullであるということです。nullでない値のみをチェックして送信する方法。これは、writeToParcel()の単純なif / elseで簡単に実行できると思いますが、Parcelとしてパラメーターを受け取るクラスのコンストラクターで同じことを実行する方法です。次のように

private Student(Parcel in) {
}

null値の送信中にいくつかの問題があります。

編集:追加されたコード

    @Override
        public void writeToParcel(Parcel dest, int flags) {
            dest.writeString(id);
            dest.writeString(name);
            dest.writeString(grade);
            dest.writeString(dateOfBirth);
            dest.writeString(place);
                //Like wise there are many other fields
                //We have to write only values which are not null
}

       private Student(Parcel in) {
            id=in.readString();
            name=in.readString();
            grade=in.readString();
            dateOfBirth=in.readString();
            place=in.readString();
            //like wise have to read all other fields
           //we have to make sure we read only values which are not null
}
4

1 に答える 1

-1

最も簡単な方法は、1 つの Globalclass.java を作成し、public static のように static arraylist を宣言ArrayList<String> arrayList = new ArrayList<String>(); し、使用したい場合Globalclass.arrayList = valueです。プロジェクトでanywareを使用します。

インテントを使用して渡す別の方法

Intent i =new Intent(this,2activity.class);
i.putStringArrayListExtra(name, arrayList);
于 2012-04-30T08:04:53.147 に答える