2011-11-04 4 views
1

나는이 있습니다이중 변환하는 방법 [] [] parcelable로

public double[][] mRoute; 

을하고 나는 그것이 Parcelable 수 있어야합니다,하지만 난 이렇게하는 가장 좋은 방법의 특정 아닙니다.

List으로 바꾼 다음 올바른 방법이 아닌 것으로 보입니다.

UPDATE :

이 @ 신용장의 대답에서 무엇을 컴파일됩니다.

out.writeInt(mRoute.length); // store the length 
    for (int i = 0; i < mRoute.length; i++) { 
     out.writeInt(mRoute[i].length); 
     out.writeDoubleArray(mRoute[i]); 
    } 
    mRoute = new double[in.readInt()][]; 
    for (int i = 0; i < mRoute.length; i++) { 
     mRoute[i] = new double[in.readInt()]; 
     in.readDoubleArray(mRoute[i]); 
    } 

나는 그것을 시험해 보지 못했다. 단지 컴파일해야한다. 이것은 시작이다.

답변

1

당신은 이중 [] []는 Parcelable 할 수 없습니다,하지만 당신은 Parcelable을 구현하고 더블 [] []가 포함 된 클래스를 만들 수 있습니다. , readFromParcel에

dest.writeInt(mRoute.length); // store the length 
for (int i = 0; i < mRoute.length; i++) { 
    dest.writeDoubleArray(mRoute[i]); 
} 

(소포에서) 수행 :

writeToParcel (소포의 최종 도착)에

는 할

mRoute = new double[][in.readInt()]; 
for (int i = 0; i < mRoute.length; i++) { 
    mRoute[i] = in.readDoubleArray(); 
} 
+0

와우,이 접근 방식에 대해 생각하지 않았다. 나는 당황 스럽지만 그렇게 간단한 해결책이다. 고맙습니다. 나는 그것을 지금 시도 할 것이다. :) –

0

나는 Parcelable에게 자신을 사용한 적이 있지만, JavaDoc를 읽은 후 나는이 일반적으로 생각 일 : 당신이
Parcel#[create|write을 사용할 수 있도록

  1. 가하는 double[]를 래핑하는 클래스 Foo implements Parcelable 만들기 ]doubleArray(),
  2. Foo[]을 래핑하는 클래스 Bar implements Parcelable을 만드십시오. 따라서
    을 사용할 수 있습니다. ]TypedArray()

6,Parcel#[create|write (죄송합니다, 제가 유용 아니라면 삭제 행복 해요 댓글에 대한 약간 깁니다.) 컴파일이 같이


(테스트를 거치지 않았으므로 no @Overrides, 경고 점 및 whot) :

public class Bar implements Parcelable { 
    private Foo[] mData; 

    // Same as above 
    public int describeContents() { 
     return 0; 
    } 

    public void writeToParcel(Parcel out, int flags) { 
     out.writeTypedArray(mData, 0); 
    } 

    public static final Creator<Bar> CREATOR = new Creator<Bar>() { 

     public Bar createFromParcel(Parcel in) { 
      return new Bar(in); 
     } 

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

    private Bar(Parcel in) { 
     mData = in.createTypedArray(Foo.CREATOR); 
    } 
} 
관련 문제