如何使用intent传递自定义对象列表,因为我使用Serializable收到错误

时间:2017-09-14 10:24:11

标签: android android-intent bundle parcelable serializable

我有一个对象列表,我想通过意图传递它。我已经实现了Serializable,但app崩溃了。

public class YoutubePlaylist implements Serializable{

@SerializedName("message")
@Expose
private String message;
@SerializedName("name")
@Expose
private String name;
@SerializedName("playlist_id")
@Expose
private String playlistId;

public String getMessage() {
    return message;
}

public void setMessage(String message) {
    this.message = message;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public String getPlaylistId() {
    return playlistId;
}

public void setPlaylistId(String playlistId) {
    this.playlistId = playlistId;
}}

我得到的错误是:

FATAL EXCEPTION: main
                                                                   Process: com.osolutions.news24, PID: 28944
                                                                   java.lang.RuntimeException: Parcel: unable to marshal value com.osolutions.news24.pojo.Item@7ffb401
                                                                       at android.os.Parcel.writeValue(Parcel.java:1418)
                                                                       at android.os.Parcel.writeList(Parcel.java:759)
                                                                       at android.os.Parcel.writeValue(Parcel.java:1365)
                                                                       at android.os.Parcel.writeMapInternal(Parcel.java:662)
                                                                       at android.os.Parcel.writeMap(Parcel.java:646)

我在下面的课程中这样传递:

public class YoutubeHorizontalPlaylistAdapter extends RecyclerView.Adapter<YoutubeHorizontalPlaylistAdapter.ViewHolder> {


private Context context;
private List<Item> itemList;
private ArrayList<YoutubePlaylist> body;
private HashMap<String, List<Item>> titleAndVideos;

public YoutubeHorizontalPlaylistAdapter(Context context,
                                        List<Item> itemList ,List<YoutubePlaylist> body, HashMap<String, List<Item>> titleAndVideos) {
    this.context = context;
    this.itemList = itemList;
    this.body = (ArrayList<YoutubePlaylist>) body;
    this.titleAndVideos = titleAndVideos;
}


@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.single_episode_item, null);
    return new ViewHolder(v);
}

@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
@Override
public void onBindViewHolder(final ViewHolder holder, final int position) {
    holder.episodeNameTxt.setText(itemList.get(position).getSnippet().getTitle());
    String imagepath = itemList.get(position).getSnippet().getThumbnails().getHigh().getUrl();
    UrlImageViewHelper.setUrlDrawable(holder.episodeImg, imagepath, R.drawable.placeholder_logo, 300000);
    holder.itemView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if (NetworkUtil.isOnline(context)) {
                   //
                Bundle bundle = new Bundle();
                bundle.putSerializable("title_body",body);

                Intent i = new Intent(context, YoutubePlayListAdapter.class);
                i.putExtra("hashmap", titleAndVideos);
                i.putExtras(bundle);

                i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                context.startActivity(i);
            } else {
                CustomAlertDialogManager.noInternetDialog(context);
            }
        }
    });

}

@Override
public int getItemCount() {
    return itemList.size();
}

public static class ViewHolder extends RecyclerView.ViewHolder {

    View v;
    private TextView episodeNameTxt;
    ImageView episodeImg;


    public ViewHolder(View itemView) {
        super(itemView);
        episodeNameTxt = itemView.findViewById(R.id.episode_name);
        episodeImg = itemView.findViewById(R.id.episode_img);
        v = itemView;

    }

}}

注意:我在构造函数中输入了arraylist。我已经尝试过Stack Overflow的其他解决方案,但无法从中获得任何解决方案。

2 个答案:

答案 0 :(得分:1)

  

使您的Item类可序列化或Parcelable。   使Item类实现java.io.Serializable接口,因为HashMap   已经是Serializable

答案 1 :(得分:0)

您需要转换 Parcelable类而不是Serializable

转到此http://www.parcelabler.com/并通过模型类并转换它。

更新模型类后 通过意图传递 Parcelable List

Intent i = new Intent(context, YoutubePlayListAdapter.class);
i.putExtra("hashmap", titleAndVideos);
i.putParcelableArrayListExtra("title_body", body);
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
context.startActivity(i);

在YoutubePlayListAdapter 活动中,您可以通过

获取
ArrayList<YoutubePlaylist> mList = getIntent().getParcelableArrayListExtra("title_body");