如何使用 intent 传递自定义对象列表,因为我在使用 Serializable 时遇到错误
How to pass List of custom object using intent as I am getting error using Serializable
我有一个对象列表,我想通过意图传递它。我也实现了 Serializable,但应用程序崩溃了。
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)
下面我是这样过的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;
}
}}
注意:我在构造函数中对 arraylist 进行类型转换。我已经尝试过 Stack Overflow 的其他解决方案,但无法从中得到任何解决方案。
Make your Item class Serializable or Parcelable.
Make the Item class implement the java.io.Serializable interface because HashMap
is already Serializable
您需要转换 Parcelable class 而不是 Serializable
转到此 http://www.parcelabler.com/ 并通过您的 模型 class 并进行转换。
更新模型后 class
通过 intent
传递 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 Activity 你可以通过
ArrayList<YoutubePlaylist> mList = getIntent().getParcelableArrayListExtra("title_body");
我有一个对象列表,我想通过意图传递它。我也实现了 Serializable,但应用程序崩溃了。
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)
下面我是这样过的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;
}
}}
注意:我在构造函数中对 arraylist 进行类型转换。我已经尝试过 Stack Overflow 的其他解决方案,但无法从中得到任何解决方案。
Make your Item class Serializable or Parcelable. Make the Item class implement the java.io.Serializable interface because HashMap is already Serializable
您需要转换 Parcelable class 而不是 Serializable
转到此 http://www.parcelabler.com/ 并通过您的 模型 class 并进行转换。
更新模型后 class 通过 intent
传递 Parcelable ListIntent 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 Activity 你可以通过
ArrayList<YoutubePlaylist> mList = getIntent().getParcelableArrayListExtra("title_body");