Domanda

I have an Parcelable object called Book and another called Author. I can't tell why the object constructor for Book is not working. The first bit of code is where i try to make it so I can send it to a parent activity. The values were checked before, and when I do the .toString() method on book, I get null Price: null

Activity Code

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

}

È stato utile?

Soluzione 2

Hi so I just gave up and made a new constructor that doesn't use Parcels.

But what was written below p.setDataPosition(0); may work as well for future users

Altri suggerimenti

     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());
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top