为什么我的用户定义对象不构造?

时间:2014-01-28 05:04:24

标签: java android parcelable

我有一个名为Book的Parcelable对象,另一个叫做Author。我不知道为什么Book的对象构造函数不起作用。代码的第一位是我尝试制作的代码,因此我可以将其发送到父活动。之前检查过这些值,当我在book上执行.toString()方法时,我得到null Price:null

活动代码

EditText editText = (EditText) findViewById(R.id.search_title);
    String title = editText.getText().toString();
    editText = (EditText) findViewById(R.id.search_author);
    String author = editText.getText().toString();
    editText = (EditText) findViewById(R.id.search_isbn);
    int isbn = Integer.parseInt(editText.getText().toString());
...    
Parcel p = Parcel.obtain();
    p.writeInt(isbn);
    p.writeString(title);
    p.writeString(author);
    p.writeString(editText.getText().toString());
    p.writeString("$15.00");
    Intent intent = new Intent();
    Book book = new Book(p);
    System.out.println(book.toString());

Book.java

public class Book implements Parcelable {

private int id;
private String title;
private ArrayList<Author> authors = new ArrayList<Author>();
//private int Aflags;
private String isbn;
private String price;

@Override
public int describeContents() {
    return 0;
}

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

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


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];
    }
};

@Override
public String toString()
{
    return title + " Price: " + price;
}

}

Author.java

public class Author implements Parcelable {

// NOTE: middleInitial may be NULL!

public String firstName;

public String middleInitial;

public String lastName;

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

private Author(Parcel in)
{
    firstName = in.readString();
    if (in.dataSize() == 2)
        middleInitial = in.readString();
    if (in.dataSize() == 1)
        lastName = in.readString();

}

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];
    }
};

@Override
public int describeContents() {
    return 0;
}

}

2 个答案:

答案 0 :(得分:0)

     Parcel p = Parcel.obtain();
        p.writeInt(isbn);
        p.writeString(title);
        p.writeString(author); // Here i think u need to write list of author and not string
        p.writeString(editText.getText().toString());
        p.writeString("$15.00");
        Intent intent = new Intent();
        Book book = new Book(p);
        System.out.println(book.toString());

    /*****Edited answer ******/
//HERE u go mate this should work, tested code
    //You need to parse the author text then

        Parcel p = Parcel.obtain();
         p.writeInt(23); // isbn
         p.writeString("sometitle");

         // List<String> data = new List<String>();//parseAuthor(author); // function dependent on what u get

        Parcel auth = Parcel.obtain();
        auth.writeString("firstname"); // firstname
        auth.writeString("middle"); // middle
        auth.writeString("lastname"); // lastname
        auth.setDataCapacity(3);
        auth.setDataPosition(0);
        Author a = Author.CREATOR.createFromParcel(auth);
        ArrayList<Author> authors = new ArrayList<Author>();
        authors.add(a);
        p.writeTypedList(authors);
        p.writeString("something");
        p.writeString("$15.00");
        p.setDataPosition(0);
        Intent intent = new Intent();
        Book book = Book.CREATOR.createFromParcel(p);
        System.out.println(book.toString());

答案 1 :(得分:-1)

嗨所以我放弃并制作了一个不使用Parcels的新构造函数。

但是p.setDataPosition(0)下面写的是什么;可能对未来的用户也有效