1

You've got an ArrayList for example:

list = new ArrayList<Obj>();
list.add(new Obj(R.drawable.img, "string", "na, na, na"));
list.add(new Obj(R.drawable.ima2, "string 2", "Na, na, na"));
list.add(new Obj(R.drawable.img3, "string 3", "Na, na, na"));
...

this List inflates into a RecyclerView.

When I click a RecyclerView Item, I want to pass only the element of the list I have clicked on to another Activity, not the whole List.

How do I do it?

This is what I have tried so far:

ItemClickSupport.addTo(recyclerView).setOnItemClickListener(new ItemClickSupport.OnItemClickListener(){
        @Override
        public void onItemClicked(RecyclerView recyclerView, int position, View v) {
            Intent intent = new Intent(ChordsListActivity.this, ChordActivity.class);
            intent.putExtra("selected", list);
            startActivity(intent);
        }
});
Petter Friberg
  • 21,252
  • 9
  • 60
  • 109
Daniele
  • 4,163
  • 7
  • 44
  • 95

3 Answers3

4

How to pass a single ArrayList elements to another Activity

For passing custom object to next Activity do following:

1. Make sure custom object class implementing Serializable or Parcelable(recommended to use) interface.

2. Use Bundle.putSerializable if implementing Serializable or use Bundle.putParcelable if implementing Parcelable interface.

Using Serializable:

Bundle bundle=new Bundle();
bundle.putSerializable("selected", chords.get(position));
intent.putExtras(bundle);

3. In next Activity use Bundle. getSerializable for getting selected object:

Intent intent = this.getIntent();
Bundle bundle = intent.getExtras();
Obj selectedObj=(Obj)bundle.getSerializable("selected");
Community
  • 1
  • 1
ρяσѕρєя K
  • 132,198
  • 53
  • 198
  • 213
  • 1
    It's recommended to use `Parcelable` in Android, because `Serializable` takes care of a lot of additional stuff, which you don't need (e.g. Serializable is designed for durable storage of objects, where the file might be read back in months or years later). Parcelable is faster than Serializable for Android’s IPC use. – Vucko Jul 17 '16 at 12:12
  • Thank you! Right now I'm using Serializable but I'm gonna change to Parcelable! – Daniele Jul 17 '16 at 12:14
  • @Vucko: Agree with you. – ρяσѕρєя K Jul 17 '16 at 12:15
  • In point 3 if I want to use Parcelable, instead of `bundle.getSerializable()` I imagine I have to use `bundle.getParcelable()` right? – Daniele Jul 17 '16 at 12:17
  • @Daniele: Yes right , but you also need to use `putParcelable ` instead of `putSerializable ` in `ChordsListActivity ` Activity – ρяσѕρєя K Jul 17 '16 at 12:18
  • Yes, It makes sense! Thank you very much for the detailed answer – Daniele Jul 17 '16 at 12:19
  • @ρяσѕρєяK please make sure that on point 2. you have `intent.putExtras(bundle)` instead of `intent.putExtra(bundle)` – Daniele Jul 17 '16 at 13:12
1
ItemClickSupport.addTo(recyclerView).setOnItemClickListener(new ItemClickSupport.OnItemClickListener(){
        @Override
        public void onItemClicked(RecyclerView recyclerView, int position, View v) {
            Intent intent = new Intent(ChordsListActivity.this, ChordActivity.class);
            // ListData is supposed to be the list which pass to your adapter

            Bundle bundle = new Bundle();
            bundle.putParcelableArrayList("itemData", list.get(position));
            intent.putExtras(bundle);
            startActivity(intent);       
        }
    });

Retrive it:

 Obj challenge = this.getIntent().getExtras().getParcelableArrayList("itemData");
Sohail Zahid
  • 8,099
  • 2
  • 25
  • 41
1

Make your Obj class a Parcelable like this

private static class Obj implements Parcelable {
    private int img;
    private String string;
    private String naNaNa;

    public Obj(@DrawableRes int img, String string, String naNaNa) {
        this.img = img;
        this.string = string;
        this.naNaNa = naNaNa;
    }

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

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeInt(this.img);
        dest.writeString(this.string);
        dest.writeString(this.naNaNa);
    }

    protected Obj(Parcel in) {
        this.img = in.readInt();
        this.string = in.readString();
        this.naNaNa = in.readString();
    }

    public static final Creator<Obj> CREATOR = new Creator<Obj>() {
        @Override
        public Obj createFromParcel(Parcel source) {
            return new Obj(source);
        }

        @Override
        public Obj[] newArray(int size) {
            return new Obj[size];
        }
    };
}

then put it into extra of intent

hadilq
  • 1,023
  • 11
  • 25