Den
Den

Reputation: 283

How to send an object of a model in Intent

I have a model and it implements the Parcelable interface, on my MainActivity I've like this

ArrayList<Model> modelList = new ArrayList<>();
for(int i = 1; i <= 5; i++)
modelList.add(new Model( "name "+ i , "number "+ i));

And I have a ListView of this modelList items.

What I'm trying to do is that when the user clicks on an item of the list I use Intent and put its extra like modelList.get(position)

I just want to put a specific object of the model, and not the whole model like intent.putParcelableArrayList("KEY", modelList)

Upvotes: 0

Views: 84

Answers (2)

Riddhi Shah
Riddhi Shah

Reputation: 3122

You can pass it in following way.

intent.putExtra("KEY",modelList.get(position));

and get it in following way in another activity

Model model=getIntent().getParcelableExtra("KEY");

Upvotes: 1

frankelot
frankelot

Reputation: 14409

You might just need to do this

Intent().putExtra("name", yourModel)

putExtra has many overloads one of them is putExtra(name : String, value : Parcelable)

Upvotes: 1

Related Questions