2012-07-06 2 views
1

Team 유형의 객체를 내 앱의 다른 Activity에 전달하려고합니다.parcelable을 사용하여 한 Android 활동에서 다른 활동으로 개체를 보내는 방법은 무엇입니까?

TreeMap<String, HashMap<Integer, ArrayList<Event>>> matchDays; 

어떻게 클래스의 나머지와 함께 중첩 된 트리 맵을 전달할 수 있습니다

Team 클래스 : 정렬 화시

public class Team implements Parcelable { 

    String teamName; 

    //Name and Link to competition of Team 
    TreeMap<String, String> competitions; 
    //Name of competition with a map of matchdays with all games to a matchday 
    TreeMap<String, HashMap<Integer, ArrayList<Event>>> matchDays; 

    public int describeContents() { 
     return 0; 
    } 

    public void writeToParcel(Parcel dest, int flags) { 
     dest.writeString(teamName); 
     dest.writeMap(competitions);  
    } 

    public static final Parcelable.Creator<Team> CREATOR = new Parcelable.Creator<Team>() { 
     public Team createFromParcel(Parcel in) { 
      return new Team(in); 
     } 

     public Team[] newArray(int size) { 
      return new Team[size]; 
     } 
    }; 

    private Team(Parcel in) { 
     teamName = in.readString(); 

     in.readMap(competitions, Team.class.getClassLoader()); 
    } 
} 

은 내가 RuntimeException을 얻을?

+0

입니까? Event 클래스는 직렬화 가능합니까? – kosa

답변

1

String, TreeMapHashMap 모두 Serializable 인터페이스를 구현합니다. Team 클래스에 Serializable을 구현하고 대신 그 액티비티간에 전달하는 것이 좋습니다. 이렇게하면 수동으로 구문 분석하지 않고도 Bundle 또는 Intent에서 직접 개체를로드 할 수 있습니다.

public class Team implements Serializable { 

    String teamName; 

    //Name and Link to competition of Team 
    TreeMap<String, String> competitions; 
    //Name of competition with a map of matchdays with all games to a matchday 
    TreeMap<String, HashMap<Integer, ArrayList<Event>>> matchDays; 

추가 구문 분석 코드가 필요하지 않습니다.

(편집 : ArrayList는 또한이 솔루션은 Event 클래스가 직렬화 가능 여부에 따라 달라집니다 Serializable을 구현합니다.) 이벤트 란

+0

나는 그것을 시도 할 것이다. 덕분에 .. 난 다른 스레드에서 그들이 Serializable은 다소 더러운 솔루션이라고 언급했기 때문에 나는 parcelable을 사용하고 있었다. – Ben

+0

굉장해 !! 위대한 작품과 천천히 비록 Serializable을 사용합니다. 감사! – Ben

관련 문제