Kevin Lourenco
Kevin Lourenco

Reputation: 53

Create a user-defined Parcelable object and implement functionality

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:

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

}

Upvotes: 0

Views: 414

Answers (2)

Shayan Pourvatan
Shayan Pourvatan

Reputation: 11948

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

Upvotes: 1

NasaGeek
NasaGeek

Reputation: 2198

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.

Upvotes: 2

Related Questions