2016-09-20 128 views
0

如何在activites之間通過ArrayList類型ArrayList<ArrayList<CustomObject>>?我已經執行與customObject模型類。當我通過像intent.putParcelableArrayListExtra("catVideoList", categoriesVideoList)它給出了錯誤。我知道如何通過ArrayListArrayList<CustomObject>如何在活動之間傳遞ArrayList <ArrayList <customtype>>

下面是我的模型類:

public class VideoContentModel implements Parcelable { 
    String videoId; 
    String videoUrl; 
    String videoTitle; 
    String videoDescription; 
    String videoThumbnailUrl; 
    String videoDuration; 
    String videoBitrate; 
    String videoPublishDate; 
    String videoCreateDate; 
    String videoUpdateDate; 
    String videoChannelId; 
    String videoChannelName; 
    String videoFeedType; 

    public VideoContentModel(String videoId, String videoUrl, String videoTitle, 
          String videoThumbnailUrl, String videoDuration, 
          String videoBitrate, String videoPublishDate, String videoCreateDate, 
          String videoUpdateDate, String videoChannelId, String videoChannelName, 
          String videoFeedType, String videoDescription) 
    { 
     this.videoId = videoId; 
     this.videoUrl = videoUrl; 
     this.videoTitle = videoTitle; 
     this.videoThumbnailUrl = videoThumbnailUrl; 
     this.videoDuration = videoDuration; 
     this.videoBitrate = videoBitrate; 
     this.videoPublishDate = videoPublishDate; 
     this.videoCreateDate = videoCreateDate; 
     this.videoUpdateDate = videoUpdateDate; 
     this.videoChannelId = videoChannelId; 
     this.videoChannelName = videoChannelName; 
     this.videoFeedType = videoFeedType; 
     this.videoDescription = videoDescription; 
    } 

    public String getVideoId() { 
     return videoId; 
    } 

    public String getVideoUrl() { 
     return videoUrl; 
    } 

    public String getVideoTitle() { 
     return videoTitle; 
    } 

    public String getVideoDescription() { 
     return videoDescription; 
    } 

    public String getVideoThumbnailUrl() { 
     return videoThumbnailUrl; 
    } 

    public String getVideoDuration() { 
     return videoDuration; 
    } 

    public String getVideoBitrate() { 
     return videoBitrate; 
    } 

    public String getVideoPublishDate() { 
     return videoPublishDate; 
    } 

    public String getVideoCreateDate() { 
     return videoCreateDate; 
    } 

    public String getVideoUpdateDate() { 
     return videoUpdateDate; 
    } 

    public String getVideoChannelId() { 
     return videoChannelId; 
    } 

    public String getVideoChannelName() { 
     return videoChannelName; 
    } 

    public String getVideoFeedType() { 
     return videoFeedType; 
    } 

    @Override 
    public int describeContents() { 
     return 0; 
    } 

    @Override 
    public void writeToParcel(Parcel dest, int flags) { 
     dest.writeString(this.videoId); 
     dest.writeString(this.videoUrl); 
     dest.writeString(this.videoTitle); 
     dest.writeString(this.videoDescription); 
     dest.writeString(this.videoThumbnailUrl); 
     dest.writeString(this.videoDuration); 
     dest.writeString(this.videoBitrate); 
     dest.writeString(this.videoPublishDate); 
     dest.writeString(this.videoCreateDate); 
     dest.writeString(this.videoUpdateDate); 
     dest.writeString(this.videoChannelId); 
     dest.writeString(this.videoChannelName); 
     dest.writeString(this.videoFeedType); 
    } 

    protected VideoContentModel(Parcel in) { 
     this.videoId = in.readString(); 
     this.videoUrl = in.readString(); 
     this.videoTitle = in.readString(); 
     this.videoDescription = in.readString(); 
     this.videoThumbnailUrl = in.readString(); 
     this.videoDuration = in.readString(); 
     this.videoBitrate = in.readString(); 
     this.videoPublishDate = in.readString(); 
     this.videoCreateDate = in.readString(); 
     this.videoUpdateDate = in.readString(); 
     this.videoChannelId = in.readString(); 
     this.videoChannelName = in.readString(); 
     this.videoFeedType = in.readString(); 
    } 

    public static final Parcelable.Creator<VideoContentModel> CREATOR = new Parcelable.Creator<VideoContentModel>() { 
     @Override 
     public VideoContentModel createFromParcel(Parcel source) { 
      return new VideoContentModel(source); 
     } 

     @Override 
     public VideoContentModel[] newArray(int size) { 
      return new VideoContentModel[size]; 
     } 
    }; 
} 
+0

添加錯誤...... – Pavya

+0

使用'putExtra'和'getParcelable'來在活動之間傳遞對象。 –

+0

@Pravin下面的錯誤顯示類型Intent中的方法putParcelableArrayListExtra(String,ArrayList <?extends Parcelable>)不適用於參數(String,ArrayList >)' –

回答

1

Parcelable是不必要在這裏,因爲你正在使用簡單對象(字符串)。 ArrayList實現了Serializable,所以你的CustomObject也應該如此。 一旦你改變它,你可以使用

intent.putExtra("name", yourArrayList); 

Bundle bundle = new Bundle(); 
bundle.putSerializable("name", yourArrayList); 

Intent intent = new Intent(context, yourActivity.class); 
intent.putExtras(bundle); 

而在你的第二個活動進行檢索。

1

你可以使用Gson庫來做到這一點,不需要實現parcelable。

假設你的ArrayList是:

ArrayList<ArrayList<CustomObject>> outer = new ArrayList<>(); 

填充它後,可以將其添加到意向如下:

Intent i = new Intent(Activity1.this, Activity2.class); 
i.putExtra("randomdata", new Gson().toJson(outer)); 

以後,您可以在活性2檢索此如下:

String extra = getIntent().getStringExtra("randomdata"); 
ArrayList<ArrayList<CustomObject>> list = new Gson().fromJson(extra, new TypeToken<ArrayList<ArrayList<CustomObject>>>(){}.getType()); 

希望它有幫助。

相關問題