2

I would like to know how pass ArrayList data through intent() in gridView.setOnItemClickListener() and get it in ShowTracksActivity.java. How is possible ?

MainActivity.java

ArrayList<Artist>artists = new ArrayList<Artist>();

// Artist 1
String[] artist_title = new String[]{ "Title 1", "Title 2","Title 3", "Title 4" };
artists.add(new Artist("Artist Name", "Album Name", "img_album", artist_title ));

// Artist 2
//...

ArtistAdapter adapter = new ArtistAdapter(this, artists);

GridView gridView = (GridView) findViewById(R.id.gridview);

gridView.setAdapter(adapter);

gridView.setOnItemClickListener(new GridView.OnItemClickListener() {
  @Override
  public void onItemClick(AdapterView<?> parent, View v, int position, long id) 
  {

    Intent ShowTrackIntent = new Intent(MainActivity.this, ShowTracksActivity.class);

    // Here ?
    // ShowTrackIntent.putExtra( ??? );

    startActivity(ShowTrackIntent);
  }
});

ShowTracksActivity.java

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // ???
    }

Thank you for your help.

EmmCall
  • 168
  • 1
  • 1
  • 13

4 Answers4

1

You can try this in MainActivity.java

gridView.setOnItemClickListener(new GridView.OnItemClickListener() {
  @Override
  public void onItemClick(AdapterView<?> parent, View v, int position, long id) 
  {
    Intent ShowTrackIntent = new Intent(MainActivity.this, ShowTracksActivity.class);
    ShowTrackIntent.putParcelableArrayListExtra("key", ArrayList<T extends Parcelable> list);
    startActivity(ShowTrackIntent );
   }
});

And Retrieve it in ShowTracksActivity.java

getIntent().getParcelableArrayListExtra("key");
Gowtham Subramaniam
  • 3,358
  • 2
  • 19
  • 31
1

Ok so here is the idea, when you click on the artist you get position of that cell inside your click listener, so now you have to use that position and take out the artist from your artists list of that position. And pass it through intent, in android for passing user defined data/object you need to either make them Serializable or Parcelable. You can refer this question

Also Intent/Bundle class has methods for this type of data passing between Activities.

Sample code:

Intent intent = new Intent(CurrentActivity.this, TargetActivity.class);
intent.putExtra(key, value); // Here key would be String and value can be    either Parcelable or Serializable
startActivity(intent);

In your case inside your item click listener:

Artist artist = artists.get(postion);
Intent intent = new Intent(MainActivity.this, ShowTracksActivity.class);
intent.putExtra("selected_artist", artist); // Here key would be String and value can be either Parcelable or Serializable
startActivity(intent);

Note: You have to make your Artist class serializable for passing it to other activity, for how to do that you can refer this Why use parcelable?, How To make class parcelable? and The easy and dirty way

Aseem Sharma
  • 1,673
  • 12
  • 19
  • It seems to be the good way, I had made a few adjustments but putExtra( ) show this error : Cannot resolve method 'putExtra(java.lang.String, com.example.android.app.Artist)' – EmmCall Mar 21 '18 at 13:12
  • Have you made your Artist Model class Serializable or Parcelable ? – Aseem Sharma Mar 21 '18 at 13:18
  • I used Parcelable has explain here [https://developer.android.com/reference/android/os/Parcelable.html], but now I have this error on **new Artist** in `artists.add(new Artist(...));` : `'Artist(android.os.Parcel)' has private access in 'com.example.android.app.Artist'` – EmmCall Mar 21 '18 at 16:26
  • In fact it break my ArtistAdapter class, I'm from PHP and there are some logic that I do not understand yet... :-/ – EmmCall Mar 21 '18 at 18:53
  • For making your artist class parcelable, you don't need to do it manually, Android Studio has plugin for that, you can generate parcelable class by it, let me know if this helps. – Aseem Sharma Mar 21 '18 at 20:38
  • Ok, so I changed my Artist.class by : `public class Artist extends ArrayList{...}` In `MainActivity.class : intent.putParcelableArrayListExtra ("key", artist);` But in ShowTracksActivity.class : `getIntent().getParcelableArrayListExtra("key");` **<- Return []**. And `getIntent().getParcelableArrayListExtra("key").get(0);` **<- Return error** – EmmCall Mar 22 '18 at 09:44
  • You do not need to extend Artist with ArrayList, let me share you a gist with proper implementation – Aseem Sharma Mar 22 '18 at 11:19
  • 1
    Checkout this [gist](https://gist.github.com/devAseemSharma/aaa321d17e8171352aec7dd52538a8b8) and do the changes as per the need. – Aseem Sharma Mar 22 '18 at 11:31
1

I made something like that in order to pass a full object what ever it is using Gson

Intent ShowTrackIntent = new Intent(MainActivity.this,ShowTracksActivity.class);
ShowTrackIntent .putExtra("ObjectValue",new Gson().toJson(yourObject));
startActivity(ShowTrackIntent);

in the second activity when you pull the object you just need to pull it like the following

MyClassModel myModel=new Gson().fromJson(getIntent().getStringExtra("ObjectValue"),MyClassModel.class);

You need to import the Powerfull Gson Library dependency, almost it is there in all projects

Basil Battikhi
  • 2,638
  • 1
  • 18
  • 34
0

You try:

In MainActivity:

intent.putExtra("list", artists);

In ShowTracksActivity:

ArrayList<Artist> list = (ArrayList<String>) 
getIntent().getSerializableExtra("list");

And Artist must is implements Serializable

Tung Tran
  • 2,885
  • 2
  • 17
  • 24