Android Intent传递Map及List类型值问题完美解决

问题:

  1. 在Android开发中传递Map时特别Map值是自定义Bean时遇到的一些问题,有时抱异常,如以下异常:
java.lang.RuntimeException: Parcelable encountered IOException writing serializable object 

原因:

Map或者Bean实体类没有做序列化,或者只实例化了一个

下面是例子:

  • 用到的工具类:CourseItemInfo.java
import java.io.Serializable;

/**
 *注意实体类必须序列化
 * Created by yongjie on 2017/1/1.
 */

public class CourseItemInfo implements Serializable {
    //章节
    private Integer section;
    //课程名称
    private String courseName;
    //视频地址
    private String localUrl;

    public Integer getSection() {
        return section;
    }

    public void setSection(Integer section) {
        this.section = section;
    }

    public String getLocalUrl() {
        return localUrl;
    }

    public void setLocalUrl(String localUrl) {
        this.localUrl = localUrl;
    }

    public String getCourseName() {
        return courseName;
    }

    public void setCourseName(String courseName) {
        this.courseName = courseName;
    }

    @Override
    public String toString() {
        return "CourseItemInfo{" +
                "section=" + section +
                ", courseName='" + courseName + '\'' +
                ", localUrl='" + localUrl + '\'' +
                '}';
    }
}
  • MapMapper实现
import android.content.Intent;

import java.io.Serializable;
import java.util.Map;

/**
 * Created by yongjie on 2017/1/5.
 */

public class MapWrapper<T extends Map & Serializable> implements Serializable {

    private final T map;

    public MapWrapper(T map) {
        this.map = map;
    }

    public T getMap() {
        return map;
    }

    public static  Intent
    putMapExtra(Intent intent, String name, T map) {

        return intent.putExtra(name, new MapWrapper<>(map));
    }

    public static  T
    getMapExtra(Intent intent, String name)
            throws ClassCastException {

        Serializable s = intent.getSerializableExtra(name);
        return s == null ? null : ((MapWrapper) s).getMap();
    }

测试方法

 public void startActivity(Map courseItemInfoMap) {
        Intent intent = new Intent(context, TransparentActivity.class);
        MapWrapper.putMapExtra(intent, "mapWrapper", courseItemInfoMap);
        startActivity(intent);
    }

public void getActivity(){
        Map courseItemInfoMap = MapWrapper.getMapExtra(getIntent(), "mapWrapper");
    }
  • ListMapper实现
import android.content.Intent;

import java.io.Serializable;
import java.util.List;

/**
 * Created by yongjie on 2017/1/5.
 */

public class ListWrapper<T extends List & Serializable> implements Serializable {

    private final T list;

    public ListWrapper(T list) {
        this.list = list;
    }

    public T getList() {
        return list;
    }

    public static List & Serializable> Intent
    putListExtra(Intent intent, String name, T list) {

        return intent.putExtra(name, new ListWrapper<>(list));
    }

    public static List & Serializable> T
    getListExtra(Intent intent, String name)
            throws ClassCastException {

        Serializable s = intent.getSerializableExtra(name);
        return s == null ? null : ((ListWrapper) s).getList();
    }
}
  • 测试方法
 public void startActivity(Map courseItemInfo) {
        Intent intent = new Intent(context, TransparentActivity.class);
        ListWrapper.putListExtra(intent, "listWrapper", courseItemInfo);
        startActivity(intent);
    }

public void getActivity(){
        List courseItemInfo = ListWrapper.getListExtra(getIntent(), "listWrapper");
    }

有更好的解决方法欢迎讨论
喜欢摄影的小伙伴可以来这个网站看看哟:视觉摄影

你可能感兴趣的:(android开发,移动开发)