简体   繁体   中英

How can I pass List<Model> from Activity to Fragment

I wanna pass List from Activity to Fragment. But I don't know model class have to implements 'Parcelable' or 'Serializable'. Also How to define implements methods. Thank you for seeing this question. Under codes are my Model class.

 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. Will I have to implements parcel able or serializable in all my Model class?

  2. When I make bundle What is a correct method? (putSerializable? putParcelable? )

  3. How can I get List in fragment.

You can implement parcelable in your model class.

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

Sending object to fragment.

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

Receiving object from fragment

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

Thers is two way to pass your List from Activity to Fragment. 1.Serializable and 2. Parcelable

  1. Will I have to implements parcel able or serializable in all my Model class? --> Yes you have to implements Parcelable or serializable in all your classes

  2. When I make bundle What is a correct method? (putSerializable? putParcelable?) --> If yor are using serializable then you have to use putSerializable otherwise putParcelable

  3. How can I get List in fragment. -->

1. Using Serializable

Sending List of object

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

Receiving list of object

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

2. Using Parcelable

Sending List of object

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

Receiving list of object

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM