【问题标题】:How can I pass this ArrayList to a new activity如何将此 ArrayList 传递给新活动
【发布时间】:2016-05-13 11:58:21
【问题描述】:

我正在尝试将此 ArrayList 传递给另一个活动。 到目前为止,我的努力都失败了。 我知道我不明白如何正确传递它。

这是我拥有的 arrayList 代码:

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 是我想传递给另一个活动的 ArrayList。 我试过这个“模板”

ArrayList<Movie> chanList= new ArrayList<>();

然后我意识到我没有正确。

有人可以帮助我获得一些洞察力的指导并帮助我理解如何正确地做到这一点吗?

谢谢!

铁螳螂7x

【问题讨论】:

标签: java android android-activity arraylist


【解决方案1】:

您必须让您的 Movie 类实现 ParcelableSerializable 以便使用意图通过活动传递它。

编辑:您的类实现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];
        }
    };
}

完成此操作后,您可以查看this question、此this tutorialthis one,了解更多信息。

TL;DR:您必须在发件人 Activity 上使用 Intent#putParcelableArrayListExtra,在第二个 Activity 上使用 Intent#getParcelableArrayListExtra

【讨论】:

  • 谢谢@Mauker。你能给我一个虚拟的例子吗?
  • 很好的答案!只是一个小错字:Creator 类使用泛型,它应该是public static final Creator&lt;Movie&gt; CREATOR... Android Studio 会为 OP 突出显示,但值得修复。另一个错字是写 id out.writeString(id);。应该是out.writeInt(id);
  • 感谢您的评论亚当!修好了!
【解决方案2】:

第一步是让你的模型Parcelablecheck here for instructions.

接下来是把你的数组列表放入intent中。

Intent intent = new Intent(this, YourNextActivity.class);
intent.putParcelableArrayListExtra("some string identifier", yourArrayList);
startActivity(intent)

然后在您的下一个活动中,使用以下方法获取数组列表:

 ArrayList<Movie> chanList = getIntent().getParcelableExtra("some string identifier");

【讨论】:

    【解决方案3】:

    你需要让你的电影类 Parcelable。如果您的电影类是 POJO,您可以使用 http://www.parcelabler.com/ 轻松做到这一点

    然后你可以直接把它放在intent中并通过它:

    Intent intent = new Intent(this, NextActivity.class);
    intent.putParcelableArrayListExtra("key_string", movieArrayList);
    startActivity(intent);
    

    【讨论】:

      【解决方案4】:

      您可以使用 Sharedprefrence 来存储列表

        SharedPreferences sharedPreferences =context.getSharedPreferences("MyPrefrence", 0); 
      
        SharedPreferences.Editor editor = sharedPreferences.edit();
      
        String movie_data = gson.toJson(mItems);
      
        editor.putString("MovieDatas", task_data);
      
        editor.commit()
      

      为了访问

        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);
      

      【讨论】:

        【解决方案5】:

        你可以用同样的方式传递一个ArrayList,

        调用 Intent 的 putExtra(String name, Serializable value) 进行存储,调用 getSerializableExtra(String name) 进行检索。

        例子:

        ArrayList<String> myList = new ArrayList<String>();
        intent.putExtra("mylist", myList);
        

        在另一个活动中:

        ArrayList<String> myList = (ArrayList<String>)getIntent().getSerializableExtra("mylist");
        

        【讨论】:

          【解决方案6】:

          对于这个解决方案,我所做的是: 我写了一个类文件来拉取流的youTubeID。 然后,我将流的 YouTubeID 作为放入 ArrayList 的变量。现在我可以选择流的 yoyTubeID 作为变量,现在一切正常!

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2020-10-02
            • 1970-01-01
            • 1970-01-01
            • 2015-01-26
            • 2014-10-30
            相关资源
            最近更新 更多