AndroidBegginer AndroidBegginer - 1 year ago 90
Android Question

Start Activity from RecyclerView.Adapter with bundled Object

I'm using RecyclerView to populate CardViews with my object "Income". Now I need one of the buttons in CardView to start new Activity and send that object to it.

Here's part of my button's onClickListener in Adapter:

holder.editButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
editAt(income);
}
});

public void editAt(Income income){
Intent i = new Intent(context,IncomeAddActivity.class);
Bundle bundle = new Bundle();

// here I want to send that "income" object

i.putExtras(bundle);
startActivity(i);

}


Is there any easy method to do this, or my approach is totally wrong?

Answer Source

You need to make your class Income implements the interface Serializable. Then you can do this:

Bundle bundle = new Bundle();
bundle.putSerializable("object", income);
intent.putExtras(bundle);
startActivity(i);