-1

So I start off by creating a custom object "Book"

Book b = new Book(id, title.getText().toString(),authors , isbn.getText().toString(), "$9.99");

All of these parameters are defined and not null. Next I take the object "b" and put it into an Intent as follows:

resultIntent.putExtra(BOOK_RESULT_KEY, b);

Still good. Retrieving the object here gets exactly what was put in, expectedly:

Book test = (Book) resultIntent.getExtras().get(BOOK_RESULT_KEY);

Finish and return to parent activity with the intent as a result:

setResult(RESULT_OK, resultIntent);
finish();

Off to the parent activity:

Book b = (Book) intent.getExtras().get(AddBookActivity.BOOK_RESULT_KEY);

Here lies the issue. All the attributes of this object book are there, except the authors[]. What I get is an array (authors[]) with the correct length, but each element in the array is now null. I am 100 percent positive it was there when it was placed into the intent. Why can I not get the contents of this array?

nwnoga
  • 577
  • 3
  • 12
  • 22

2 Answers2

3

You need to make your Book class Parcelable and then you can pass it as Parcelable array into Bundle and get it directly from Bundle.

Check out the Simple Parcelable Example

Assuming your code

  public class Book implements Parcelable{

    private String id;
    private String title;
    private String authors;
    private String isbn;
    private String price;
    // Constructor
    public Student(String id, String title, String authors,String isbn,String price){
        this.id = id;
        this.title= title;
        this.authors = authors;
        this.isbn=isbn;
        this.price=price;
   }

    ......................................
       // Parcelling part
   public Book(Parcel in){
       String[] data = new String[5];

       in.readStringArray(data);
       this.id = data[0];
       this.title= data[1];
       this.authors= data[2];
       this.isbn= data[3];
       this.price= data[4];
   }

   @Оverride
   public int describeContents(){
       return 0;
   }

   @Override
   public void writeToParcel(Parcel dest, int flags) {
       dest.writeStringArray(new String[] {this.id,
                                           this.title,
                                           this.authors,this.isbn,this.price});
   }
   public static final Parcelable.Creator CREATOR = new Parcelable.Creator() {
       public Book createFromParcel(Parcel in) {
           return new Book(in); 
       }

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

Now after creating Parcelable class you can pass data as below:

   resultIntent.putExtra(BOOK_RESULT_KEY,new Book(id, title.getText().toString(),authors , isbn.getText().toString(), "$9.99"));

Get the data from Bundle as below:

Bundle data = getIntent().getExtras();

 Book b = (Book)data.getParcelable(AddBookActivity.BOOK_RESULT_KEY);
GrIsHu
  • 29,068
  • 10
  • 64
  • 102
0

Use book as implement Serialisable and getserialisable object in next activity.

Amish
  • 117
  • 4
  • 1
    If he done with Parcelable then what is problem? And why Serializable, if Android gives Parcelable? – Pankaj Kumar Feb 07 '14 at 06:50
  • both are same as functional but i'm doing it with using serialisable.parcelable is also good :) – Amish Feb 07 '14 at 07:04
  • i am sorry i am using it.but can you give me suggetion why we are not using serialisation.so i can improve it to parcelable.. – Amish Feb 07 '14 at 07:23
  • Read http://blog.3pillarglobal.com/parcelable-vs-java-serialization-android-app-development – Pankaj Kumar Feb 07 '14 at 07:26