嵌套 Parcelable 无法工作 android

nest Parcelable can not work android

我有一本 class 和一位 作者 class 是这样的:

public class Book implements Parcelable{

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

public Book(String title, Author[] authors, String isbn, String price) {
    this.title = title;
    this.authors = authors;
    this.isbn = isbn;
    this.price = price;
}
//setter and getter....

public int describeContents(){
    return 0;
}

public Book(Parcel in)
{
    String[] data = new String[3];
    in.readStringArray(data);
    this.title = data[0];
    this.price = data[1];
    this.isbn = data[2];

    this.authors = in.readParcelable(Author.class.getClassLoader());
}

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

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

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

作者 class:

public class Author implements Parcelable {

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

public Author(String f, String l)
{
    this.firstName = f;
    this.middleInitial = "";
    this.lastName = l;
}

public Author(String f, String m, String l)
{
    this.firstName = f;
    this.middleInitial = m;
    this.lastName = l;
}

//setter and getter....

public int describeContents(){
    return 0;
}

public Author(Parcel in)
{
    String[] data = new String[3];

    in.readStringArray(data);
    this.firstName = data[0];
    this.middleInitial = data[1];
    this.lastName = data[2];
}

public void writeToParcel(Parcel dest, int flags) {
    dest.writeString(firstName);
    dest.writeString(middleInitial);
    dest.writeString(lastName);
}

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

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

然后扩展 arrayadapter:

public class ViewHolderAdapter extends ArrayAdapter<Book>
{
    Context context;
    int layoutResourceId;
    ArrayList<Book> books;

    public ViewHolderAdapter(Context context, int layoutResourceId, ArrayList<Book> books) {
        super(context, layoutResourceId, books);
        this.layoutResourceId = layoutResourceId;
        this.context = context;
        this.books = books;
    }


    public View getView(int position, View convertView, ViewGroup parent)
    {
        ViewHolder holder;
        if (convertView == null)
        {
            LayoutInflater inflater = getLayoutInflater();
            convertView =inflater.inflate(R.layout.cart_row, parent, false);
            holder = new ViewHolder();
            holder.title = (TextView) convertView.findViewById(R.id.cart_row_title);
            holder.authors = (TextView) convertView.findViewById(R.id.cart_row_author);
            convertView.setTag(holder);
        }
        else
        {
            holder = (ViewHolder) convertView.getTag();
        }

        Book book= books.get(position);

        holder.title.setText(book.getTitle());
        Author[] authors = book.getAuthors();
        String authors_str = "";
        for(int i = 0; i < authors.length; i++)
        {
            authors_str += authors[i].firstName + " "
                    + authors[i].middleInitial + " "
                    + authors[i].lastName + " ";
        }
        holder.authors.setText(authors_str);

        return convertView;
    }
}

当我运行它时:

ListView listView = (ListView) findViewById(R.id.books_list);
viewHolderAdapter = new ViewHolderAdapter(this, R.layout.cart_row, shoppingCart);
listView.setAdapter(viewHolderAdapter);

发生这样的错误:

错误:(56, 35) 错误:类型不兼容 必填:作者[]

找到:INT#1

其中 INT#1 是交集类型:

INT#1 扩展了 Author[],Parcelable

感谢您的宝贵时间。

在 Book 中,您有一组作者,而不是单个作者。要正确读回,您应该使用 readParcelableArray

改变

this.authors = in.readParcelable(Author.class.getClassLoader());

this.authors = in.readParcelableArray(Author.class.getClassLoader());

您的 Book 构造函数应该类似于

public Book(Parcel in) {
    id  = in.readInt();
    this.title = in.readString();
    this.price = in.readString();
    this.isbn = in.readString();
    this.authors = in.readParcelableArray(Author.class.getClassLoader());
}

或者您可以使用

dest.writeTypedArray(authors, flags);

写你的数组和

authors = in.createTypedArray(Author.CREATOR);

回读