如何将List <model>从Activity传递给Fragment

时间:2018-06-11 09:49:16

标签: java android parcelable serializable

我想将List从Activity传递给Fragment。但我不知道模型类必须实现'Parcelable'或'Serializable'。另外如何定义实现方法。谢谢你看到这个问题。代码下是我的Model类。

 public class Artist {

private String artistName;
private String aritstImgPath;
private List<Album> albumList;

public Artist() {

}

public Artist(String artistName, String aritstImgPath, List<Album> albumList) {
    this.artistName = artistName;
    this.aritstImgPath = aritstImgPath;
    this.albumList = albumList;
}


public String getArtistName() {
    return artistName;
}

public void setArtistName(String artistName) {
    this.artistName = artistName;
}

public String getAritstImgPath() {
    return aritstImgPath;
}

public void setAritstImgPath(String aritstImgPath) {
    this.aritstImgPath = aritstImgPath;
}

public List<Album> getAlbumList() {
    return albumList;
}

public void setAlbumList(List<Album> albumList) {
    this.albumList = albumList;
}

@Override
public String toString() {
    return "Artist{" +
            "artistName='" + artistName + '\'' +
            ", aritstImgPath='" + aritstImgPath + '\'' +
            ", albumList=" + albumList +
            '}';
}

}

public class Album {
private String albumTitle;
private String albumImgPath;
private List<Song> songList;

public Album() {
}

public Album(String albumTitle, String albumImgPath, List<Song> songList) {
    this.albumTitle = albumTitle;
    this.albumImgPath = albumImgPath;
    this.songList = songList;
}

public String getAlbumTitle() {
    return albumTitle;
}

public void setAlbumTitle(String albumTitle) {
    this.albumTitle = albumTitle;
}

public String getAlbumImgPath() {
    return albumImgPath;
}

public void setAlbumImgPath(String albumImgPath) {
    this.albumImgPath = albumImgPath;
}

public List<Song> getSongList() {
    return songList;
}

public void setSongList(List<Song> songList) {
    this.songList = songList;
}

@Override
public String toString() {
    return "Album{" +
            "albumTitle='" + albumTitle + '\'' +
            ", albumImgPath='" + albumImgPath + '\'' +
            ", songList=" + songList +
            '}';
}
}

public class Song {
private String songTitle;
private String playTime;
private String assPath;
private String ampPath;

public Song() {
}

public Song(String songTitle, String playTime, String assPath, String ampPath) {
    this.songTitle = songTitle;
    this.playTime = playTime;
    this.assPath = assPath;
    this.ampPath = ampPath;
}

public String getSongTitle() {
    return songTitle;
}

public void setSongTitle(String songTitle) {
    this.songTitle = songTitle;
}

public String getPlayTime() {
    return playTime;
}

public void setPlayTime(String playTime) {
    this.playTime = playTime;
}

public String getAssPath() {
    return assPath;
}

public void setAssPath(String assPath) {
    this.assPath = assPath;
}

public String getAmpPath() {
    return ampPath;
}

public void setAmpPath(String ampPath) {
    this.ampPath = ampPath;
}

@Override
public String toString() {
    return "Song{" +
            "songTitle='" + songTitle + '\'' +
            ", playTime='" + playTime + '\'' +
            ", assPath='" + assPath + '\'' +
            ", ampPath='" + ampPath + '\'' +
            '}';
}
}
  1. 我是否必须在所有Model类中实现parcel able或serializable?

  2. 当我制作包时什么是正确的方法? (putSerializable?putParcelable?)

  3. 如何在片段中获取列表。

2 个答案:

答案 0 :(得分:2)

您可以在模型类中实现parcelable。

public class ModelClass implements Parcelable {

        public ModelClass(Parcel in) {
            super(); 
            readFromParcel(in);
        }

        public static final Parcelable.Creator<ModelClass> CREATOR = new Parcelable.Creator<ModelClass>() {
            public ModelClass createFromParcel(Parcel in) {
                return new ModelClass (in);
            }

            public ModelClass [] newArray(int size) {

                return new ModelClass [size];
            }

        };

        public void readFromParcel(Parcel in) {
          Value1 = in.readInt();
          Value2 = in.readInt();
          Value3 = in.readInt();

        }
        public int describeContents() {
            return 0;
        }

        public void writeToParcel(Parcel dest, int flags) {
            dest.writeInt(Value1);
            dest.writeInt(Value2);  
            dest.writeInt(Value3);
       }
    }

将对象发送到片段。

ArrayList<ModelClass> arraylist = new Arraylist<>();  
Bundle bundle = new Bundle();  
bundle.putParcelableArrayList("arraylist", arraylist);

从片段

接收对象
Bundle extras = getIntent().getExtras();  
ArrayList<ModelClass> arraylist  = extras.getParcelableArrayList("arraylist");  
ModelClass model= arrayList[0];

答案 1 :(得分:2)

Thers是将List从Activity传递到Fragment的两种方法。 1.Serializable和2. Parcelable

  1. 我是否必须在我的所有Model类中实现parcel able或serializable? - &GT; 是的,你必须在所有类中实现Parcelable或serializable

  2. 当我制作包时什么是正确的方法? (putSerializable?putParcelable?) - &gt; 如果您使用的是serializable,则必须使用putSerializable,否则使用putParcelable

  3. 如何在片段中获取List。 - &GT;

  4. <强> 1。使用Serializable

    发送对象列表

        Bundle bundle = new Bundle();
        bundle.putSerializable("key",arraylist);
    

    接收对象列表

     List<Model> = (List<Model>)  getArguments().getSerializable("key);
    

    <强> 2。使用Parcelable

    发送对象列表

    Bundle bundle = new Bundle();  
    bundle.putParcelableArrayList("key", arraylist);
    

    接收对象列表

    List<Model>  arraylist  = getArguments().getParcelableArrayList("arraylist");