2

I have an Arraylist of custom objects. I am trying to pass it from one java file to another. I tried the putExtra method and the Parcelable option; but I'm unable to understand Parcelable.

Here's my custom object:

public class AddValues implements Serializable{
    public int id;
    String value;

    public AddValues(int id, String value)
    {
        this.id = id;
        this.value = value;
    }

    @Override
    public String toString() {
        String result = "id = "+id+","+" value = "+value;
        return result;
    }

    public  int getid()
    {
        return  this.id;
    }

    public String getvalue()
    {
        return this.value;
    }
}

And here's my code to send the ArrayList:

Intent intent = new Intent(BluetoothLeService.this,HomePageFragment.class);
intent.putExtra("id", data_id);
intent.putExtra("value", list);

Here "list" refers to the ArrayList.

Yazan
  • 6,074
  • 1
  • 19
  • 33
Santu Reddy
  • 87
  • 1
  • 9

2 Answers2

3

Serializable and Parcelable are not the same thing, although they serve the same purpose. This post contains an example of the Parcelable object.

The pattern employed should be followed for all Parcelable objects you want to create, changing only the writeToParcel and AddValues(Parcel in) methods. These two methods should mirror eachother, if writeToParcel writes an int then a string, the constructor should read an int then a string

Parcelable

public class AddValues implements Parcelable{
    private int id;
    private String value;

    // Constructor
    public AddValues (int id, String value){
        this.id = id;
        this.value= value;
   }

   // Parcelling part
   public AddValues (Parcel in){
       this.id = in.readInt();
       this.value= in.readString();
   }

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

   @Override
   public void writeToParcel(Parcel dest, int flags) {
       dest.writeInt(id);
       dest.writeString(value);
   }

   public final Parcelable.Creator CREATOR = new Parcelable.Creator() {
       public AddValues createFromParcel(Parcel in) {
           return new AddValues(in); 
       }

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

}

Adding the list to an Intent extra should be simple now

Put List extra

ArrayList<AddValue> list = new ArrayList<>();
Intent intent = new Intent(BluetoothLeService.this,HomePageFragment.class);
intent.putExtra("arg_key", list);

Get List extra

ArrayList<AddValues> list = (ArrayList<AddValues>) intent.getSerializableExtra("arg_key");

As an aside, you can use the Pair<Integer, String> object instead of creating an AddValues object. This doesn't affect the answer, but might be nice to know.

Community
  • 1
  • 1
Kevin
  • 1,626
  • 16
  • 30
  • Thanks Kevin.When I pass the intent.putExtra method, it gives an error Error:(220, 74) error: incompatible types: List cannot be converted to ArrayList extends Parcelable> – Santu Reddy Mar 24 '16 at 09:15
  • No. I'll update my answer, found 2 issues in it. `List` cannot be used, as it isn't `Serializable` and there are minor changes in the Parcelable class. – Kevin Mar 24 '16 at 09:25
  • Alright, awaiting response. Thanks =) – Santu Reddy Mar 24 '16 at 09:27
  • The error still persists. And the error is in PutList extra. Same error. – Santu Reddy Mar 24 '16 at 09:49
  • @SantuReddy I just ran it again without making any changes. It printed out successfully. Make sure your code is the same, and if it still doesn't work; try cleaning the project. – Kevin Mar 24 '16 at 09:58
  • Its giving me the syntax error in PutList Extra. I cleaned the project and ran it again. – Santu Reddy Mar 24 '16 at 11:14
  • @SantuReddy Did you change `List` to be `ArrayList`? `ArrayList list = new ArrayList<>();` – Kevin Mar 24 '16 at 11:15
  • Yes of course. I have used the Arraylist itself. With AddValue as the type. – Santu Reddy Mar 26 '16 at 05:53
1

You can pass object instances in the intent extra using the putExtra("key", Serializable value) variant of the Intent#putExtra() method, which you are already doing here.

Intent intent = new Intent(BluetoothLeService.this,HomePageFragment.class);
intent.putExtra("id", data_id);
intent.putExtra("value", list);

Now for getting this data in the other Activity you need to use getSerializableExtra("key") method.

list = getIntent().getSerializableExtra("value");

But if you want to use Parcelable see @kevins answer.

K Neeraj Lal
  • 6,768
  • 3
  • 24
  • 33