bhaskar
bhaskar

Reputation: 1080

How to pass List of custom object using intent as I am getting error using Serializable

I have a list of object and I want to pass it through intent. I have implemented Serializable as well but app is crashing.

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

The error that I am getting is:

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)

I am passing like this in the following class:

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;

    }

}}

Note: I am type casting the arraylist in the constructor. I have already tried other solutions from Stack Overflow but could not get any solution out of it.

Upvotes: 0

Views: 659

Answers (2)

Mohit Suthar
Mohit Suthar

Reputation: 9375

You need to convert Parcelable class instead of Serializable

Go to this http://www.parcelabler.com/ and past your model class and convert it.

After update your model class pass Parcelable List via intent

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

In YoutubePlayListAdapter Activity you can get by

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

Upvotes: 0

Kunu
Kunu

Reputation: 5134

Make your Item class Serializable or Parcelable. Make the Item class implement the java.io.Serializable interface because HashMap is already Serializable

Upvotes: 1

Related Questions