Reputation: 827
I am trying to pass this ArrayList to another activity. So far my efforts have been a failure. I know that I am not understanding how to pass it correctly.
Here is the arrayList code that I have:
public static ArrayList<Movie> getMovieItems() {
if(mItems == null) {
mItems = new ArrayList<>();
Movie movie1 = new Movie();
movie1.setId(1);
movie1.setTitle("Title1");
movie1.setStudio("studio1");
movie1.setDescription("description1");
movie1.setCardImageUrl("http://3.bp.blogspot.com/-ZKjKucsPdzI/TudWC99CE_I/AAAAAAAAAD8/qvWdDtw5IW0/s1600/%2528393%2529.jpg");
//movie1.setVideoUrl("http://corochann.com/wp-content/uploads/2015/07/MVI_0949.mp4");
/* Google sample app's movie */
//movie1.setVideoUrl("http://www.youtube.com/embed/VopbGPJVkzM");
//movie1.setVideoUrl("http://live.gph.gov.sa/makkahlive/");
//movie1.setVideoUrl("http//livestreaming2.itworkscdn.net/squranlive/squran_7200p");
/// --- try this
//String url = "http://www.youtube.com/embed/VopbGPJVkzM";
//movie1.setVideoUrl("http://commondatastorage.googleapis.com/android-tv/Sample%20videos/Demo%20Slam/Google%20Demo%20Slam_%2020ft%20Search.mp4");
movie1.setVideoUrl("http//livestreaming2.itworkscdn.net/squranlive/squran_360p");
mItems.add(movie1);
Movie movie2 = new Movie();
movie2.setId(2);
movie2.setTitle("Title2");
movie2.setStudio("studio2");
movie2.setDescription("description2");
movie2.setCardImageUrl("http://www.questionsonislam.com/sites/default/files/mescid-i-nebevi.jpg");
//movie2.setVideoUrl("http://corochann.com/wp-content/uploads/2015/07/MVI_0962.mp4");
/* Google sample app's movie */
movie2.setVideoUrl("http://www.youtube.com/embed/4OoKpZWJASY");
mItems.add(movie2);
Movie movie3 = new Movie();
movie3.setId(3);
movie3.setTitle("Title3");
movie3.setStudio("studio3");
movie3.setDescription("description3");
movie3.setCardImageUrl("http://2.bp.blogspot.com/-ju9FXyjDFqI/TgY7uVabgxI/AAAAAAAAADw/-qSdxfyHosM/s1600/masjid+qiblatain+%25283%2529.gif");
//movie3.setVideoUrl("http://corochann.com/wp-content/uploads/2015/07/MVI_1112.mp4");
movie3.setVideoUrl("http://www.youtube.com/embed/4OoKpZWJASY");
mItems.add(movie3);
}
return mItems;
}
mItems is the ArrayList that I want to pass to another activity. I tried this "template"
ArrayList<Movie> chanList= new ArrayList<>();
Then I realized that I don't have it correct.
Can someone help me out with some insight tutelage and help me understand how to do this correctly?
Thanks!
ironmantis7x
Upvotes: 1
Views: 209
Reputation: 827
For this solution what I did was: I wrote a class file to pull the youTubeID of the stream. I then made the YouTubeID of the stream a variable that I put into the ArrayList. Now I can chose the yoyTubeID of the stream as a variable and all works now!
Upvotes: 0
Reputation: 1061
you can use Sharedprefrence to store list
SharedPreferences sharedPreferences =context.getSharedPreferences("MyPrefrence", 0);
SharedPreferences.Editor editor = sharedPreferences.edit();
String movie_data = gson.toJson(mItems);
editor.putString("MovieDatas", task_data);
editor.commit()
and for access
SharedPreferences sharedPreferences = context.getSharedPreferences("MyPrefrence", 0);
String json=sharedPreferences.getString("MovieDatas","");
Type type=new TypeToken<ArrayList<Movie>>(){}.getType();
mItems = new ArrayList<Movie>();
mItems=New Gson().fromjson(json,type);
Upvotes: 0
Reputation: 11497
You'll have to make your Movie
class implement Parcelable
or Serializable
in order to pass it through Activities using intents.
EDIT: An example of your class implementing Parcelable
:
public class Movie implements Parcelable {
// I'm assuming that's what you have inside your class.
private int id;
private String title;
private String studio;
private String description;
private String imageURL;
private String videoURL;
public Movie() { }
// Getters and Setters here
//...
// Read the Parcel in the same order you wrote it.
public Movie(Parcel in) {
this.id = in.readInt();
this.title = in.readString();
this.studio = in.readString();
this.description = in.readString();
this.imageURL = in.readString();
this.videoURL = in.readString();
}
// Some dumb method, just leave it as it is.
@Override
public int describeContents() {
return 0;
}
// Write your data to Parcel, remember, you'll have to read in the same order you wrote here.
@Override
public void writeToParcel(Parcel out, int flags) {
out.writeInt(id);
out.writeString(title);
out.writeString(studio);
out.writeString(description);
out.writeString(imageURL);
out.writeString(videoURL);
}
public static final Creator<Movie> CREATOR = new Creator() {
@Override
public Movie createFromParcel(Parcel parcel) {
return new Movie(parcel);
}
@Override
public Movie[] newArray(int i) {
return new Movie[i];
}
};
}
After you do that, you can check this question, this this tutorial and this one, for further information.
TL;DR: You'll have to use Intent#putParcelableArrayListExtra
on the sender Activity, and Intent#getParcelableArrayListExtra
on the second Activity.
Upvotes: 2
Reputation: 726
You need to make your movie class Parcelable. If your movie class is a POJO you can easily do it using http://www.parcelabler.com/
Then you can do directly put it in intent and pass it through :
Intent intent = new Intent(this, NextActivity.class);
intent.putParcelableArrayListExtra("key_string", movieArrayList);
startActivity(intent);
Upvotes: 1
Reputation: 1304
First step is to make your model Parcelable, check here for instructions.
Next is to put your array list in the intent.
Intent intent = new Intent(this, YourNextActivity.class);
intent.putParcelableArrayListExtra("some string identifier", yourArrayList);
startActivity(intent)
Then in your next activity, get the array list using this:
ArrayList<Movie> chanList = getIntent().getParcelableExtra("some string identifier");
Upvotes: 1