Question

new to android programming. I have 2 objects that must be Parcelable called Book and Author. I am fairly certain I prepared them correctly. I was hoping for some help with:

  • Creating a Book
  • sending them to a new activity

    public class Book implements Parcelable {
    
    public int id;
    public String title;
    public ArrayList<Author> authors = new ArrayList<Author>();
    public int Aflags;
    public String isbn;
    public 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);
    }
    
    private 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;
    }
    
     }
    

    Now Author:

    public class Author implements Parcelable {
    
    // TODO Modify this to implement the Parcelable interface.
    // I think I was able to accomplish this
    // 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();
        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;
    }
    
    
      }
    

I don't know why I'm stumped but I can't find out how to implement my Parcel to make the book and go any further. This is the activity that is trying to make the book then send it to another activity:

public class AddBookActivity extends Activity {

// Use this as the key to return the book details as a Parcelable extra in the result intent.
public static final String BOOK_RESULT_KEY = "book_result";

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.add_book);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    super.onCreateOptionsMenu(menu);
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.search_menu, menu);
    // COMPLETED TODO provider SEARCH and CANCEL options
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    super.onOptionsItemSelected(item);
    // TODO
    switch (item.getItemId()) {
    case R.id.search:
    // SEARCH: return the book details to the BookStore activity
        this.searchBook();
    return true;
    case R.id.cancel:
        new AlertDialog.Builder(this)
        .setTitle("Cancel Search")
        .setMessage("Are you sure you want to cancel your search?")
        .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) { 
                AddBookActivity.this.finish();
            }
         })
        .setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) { 
                // do nothing
            }
         })
        .setIcon(R.drawable.ic_dialog_alert)
         .show();
    // CANCEL: cancel the search request
    return true;
    }
    return false;
}

public Book searchBook(){
    /*
     * Book object with the search criteria and return that.
    */
    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");
    return null;
}

}

Was it helpful?

Solution

If you're trying to send the parcelable object to a new Activity through the use of an Intent, it should be as simple as calling intentobject.putExtra("key", parcelableobject). Android will handle the rest for you because your object implements the Parcelable interface.

OTHER TIPS

for sending data use:

Intent intent = new Intent(this,NewActivity.class);
intent.putExtra("listOfBook", bookList); // sending list
intent.putExtra("objOfBook", bookObj);  // sending obj
startActivity(intent);

and for getting that:

ArrayList<Book> myList = getIntent().getParcelableExtra("listOfBook"); // getting list
Book bookObj = getIntent().getParcelableExtra("objOfBook"); // getting obj

edit following function with my code:

public Book searchBook(){
    /*
     * Book object with the search criteria and return that.
    */
    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());
    Book b = new Book();
    b.SetIsbn(isbn);
    b.SetTitle(title);
    b.SetAuthor(author);
    b.SetPrice("$15.00")
    // i don't know what for is last line
    return b;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top