2017-02-08 13 views
0

バンドルで別のパーセルオブジェクト内でパーセルされた配列オブジェクトを送受信しようとしています。 PARCELED BookオブジェクトBook.javaAndroid - 入れ子になったパーセルオブジェクトを送受信する方法 "バンドル"

public class Book implements Parcelable { 

public int id; 
public String title; 
public Author[] authors; 
public String isbn; 
public String price; 

private Book(Parcel in) { 
    this.id = in.readInt(); 
    this.title = in.readString(); 
    in.readTypedArray(authors, Author.CREATOR); 
    this.isbn = in.readString(); 
    this.price = in.readString(); 
} 

public void writeToParcel(Parcel out, int flags) { 
    out.writeInt(this.id); 
    out.writeString(this.title); 
    out.writeArray(this.authors); 
    out.writeString(this.isbn); 
    out.writeString(this.price); 
} 

public int describeContents(){ 
    return 0; 
} 

public static final Parcelable.Creator<Book> CREATOR = new Parcelable.Creator<Book>() { 
    @Override 
    public Book createFromParcel(Parcel in) { 
     return new Book(in); 
    } 
    @Override 
    public Book[] newArray(int size) { 
     return new Book[size]; 
    } 
}; 

public Book(int id, String title, Author[] author, String isbn, String price) { 
    this.id = id; 
    this.title = title; 
    this.authors = author; 
    this.isbn = isbn; 
    this.price = price; 
} 

:PARCELED ARRAYはAuthor.java

public class Author implements Parcelable { 

public String firstName; 
public String middleInitial; 
public String lastName; 

public Author() { 
} 

@Override 
public void writeToParcel(Parcel out, int flags) { 
    out.writeString(this.firstName); 
    out.writeString(this.middleInitial); 
    out.writeString(this.lastName); 
} 

private Author(Parcel in) { 
    this.firstName = in.readString(); 
    this.middleInitial = in.readString(); 
    this.lastName = in.readString(); 
} 

public int describeContents(){ 
    return 0; 
} 

public static final Parcelable.Creator<Author> CREATOR = new Parcelable.Creator<Author>() { 
    @Override 
    public Author createFromParcel(Parcel in) { 
     return new Author(in); 
    } 
    @Override 
    public Author[] newArray(int size) { 
     return new Author[size]; 
    } 
}; 

public String toString() { 
    StringBuffer sb = new StringBuffer(); 
    if (firstName != null && !"".equals(firstName)) { 
     sb.append(firstName); 
     sb.append(' '); 
    } 
    if (middleInitial != null && !"".equals(middleInitial)) { 
     sb.append(middleInitial); 
     sb.append(' '); 
    } 
    if (lastName != null && !"".equals(lastName)) { 
     sb.append(lastName); 
    } 
    return sb.toString(); 
} 

}

コードをオブジェクト:アレイは

コード長の問題を有する

常に、 }

コールとオブジェクトを作成します。

Book new_book = new Book(1, title, authors.parseAuthors(author), isbn, "35$"); 
Intent i = new Intent(this, BookActivity.class); 
Bundle bundle = new Bundle(); 
bundle.putParcelable("key", new_book); 
i.putExtras(bundle); 
startActivity(i); 

をリモートアクティビティ "BOOK ACTIVITY.CLASS"

Bundle bundle = getIntent().getExtras(); 
Book book = bundle.getParcelable("key"); 

任意のアイデアを、どのように私はこの問題を解決することができますか。?バンドル内でネストされています。また、私は著者オブジェクト上でパーセル可能な配列を読み込むための正しい方法を使用していると確信しています。

おかげで、

+0

この「out.writeArray()」を「out.writeTypedArray()」に変更しました。私はそれを修正しました –

答えて

0

ではなくParcel.createTypedArray(ClassLoaderを...)を使用してみてください。私が見ているところでは、readTypedArrayを使用していますが、読み込む前に出力先配列を初期化していません。このようにするには、配列の現在の長さをParcelに渡し、配列を読み込む前に適切なサイズで配列を初期化する必要があります。

+0

私はcreateTypedArray "this.authors = in.createTypedArray(Author.CREATOR)を使用しようとしましたが、私が期待していたreasultsは間違っています.."著者名は別の変数にシフトを開始 "。 –

関連する問題