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.

1
  • What is you issue? Show the code where you take list from intent. Commented Mar 24, 2016 at 8:25

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

8 Comments

Thanks Kevin.When I pass the intent.putExtra method, it gives an error Error:(220, 74) error: incompatible types: List<AddValues> cannot be converted to ArrayList<? extends Parcelable>
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.
Alright, awaiting response. Thanks =)
The error still persists. And the error is in PutList extra. Same error.
@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.
|
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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.