2011-05-02 4 views
16

JSON 인덱스를 보유하는 ConstantData 클래스가 있으므로 엑스트라와 함께 액티비티간에 전달해야합니다. 하지만 그 전에는 먼저이 클래스의 객체에 Parcelable을 구현해야합니다.Android : 내 개체에 Parcelable을 구현하는 방법?

제 질문은, 여기 내 수업 내에서 객체를 선언하고 모든 객체를 변수 안에 넣어야하는 것입니다.

나는 초보자이며 지금은 완전히 단서가 없습니다. 고맙습니다. 아래 코드를 자유롭게 수정하십시오.

ConstantData.java

public class ConstantData{ 

     public static String project_title = "project title"; 
     public static String organization_title = "organization title"; 
     public static String keyword = "keyword"; 
     public static String short_code = "short code"; 
     public static String project_description = "description"; 
     public static String smallImageUrl = "smallImageUrl"; 
     public static String bigImageUrl = "bigImageUrl"; 
     public static String price= "price"; 
     public static String country= "country"; 



     public static ArrayList<Project> projectsList = new ArrayList<Project>(); 


     public int describeContents() { 
      return 0; 
     } 

     public void writeToParcel(Parcel out, int flags) { 
      out.writeString(project_title); 
      out.writeString(organization_title); 
      out.writeString(keyword); 
      out.writeString(short_code); 
      out.writeString(project_description); 
      out.writeString(smallImageUrl); 
      out.writeString(bigImageUrl); 
      out.writeString(price); 
      out.writeString(country); 
     } 

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

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

     private ConstantData(Parcel in) { 
      project_title = in.readString(); 
      organization_title = in.readString(); 
      keyword = in.readString(); 
      short_code = in.readString(); 
      project_description = in.readString(); 
      smallImageUrl = in.readString(); 
      bigImageUrl = in.readString(); 
      price = in.readString(); 
      country = in.readString(); 
     } 
    } 

경우 내 질문은 당신이이 질문에 찾아 볼 수 충분히 명확하지 않습니다 : 저기 myParcelableObject을 썼다 How to send an object from one Android Activity to another using Intents?

을, 난 그냥 방법을 모르는 parcelable 객체를 만든다.

편집 Project.java이

public class Project { 

    public String project_title; 
    public String organization_title; 
    public String keyword; 
    public String short_code; 
    public String project_description; 
    public String smallImageUrl; 
    public String bigImageUrl; 
    public String price; 
    public String country; 
} 
+0

내부 솔루션 : http://stackoverflow.com/questions/4049627/parcelable-and-inheritance-in-android –

+0

감사합니다! 그래서 제 경우에는 '추상 추상 클래스는 파싱 가능'권리 만 필요합니다. 나는 'public class B extends A'가 필요하지 않습니다. 맞습니까? – hectichavana

+0

언급되지 않은 한 가지 점은 JSON/XML 객체로 이미 데이터가 있고 이미 파서를 작성했다면 JSON 또는 XML 객체를 전달하는 것이 더 쉽습니다 (1-2 개 이상을 전달하지 않는 경우). 시간 –

답변

17

당신은

public class ConstantData implements Parcelable { 

    public void writeToParcel(Parcel out, int flags) { 
     out.writeString(project_title); 
    .... 
     out.writeString(country); 
     out.writeList(projectsList); // <<-- 
    } 

    private ConstantData(Parcel in) { 
     project_title = in.readString(); 
     .... 
     country = in.readString(); 
     projectsList = in.readList(); 

내가 일할 수있는 projectsList의 쓰기에 대한 생각 Parcelable 인터페이스를 구현할 필요가의 Project 클래스도 필요 Parcelable을 구현하십시오.

예를 들어 this class.

+0

감사합니다! 한 가지 더, JSON 변수를 포함하는 'Project'클래스가 있는데, 어떻게 ConstantData Parcelable 클래스에서 'Project'를 정의 할 수 있습니까? 예를 들어 위의 코드에서 프로젝트를 배열 안에 넣었습니다. "public static ArrayList projectsList = new ArrayList ();" – hectichavana

+0

@Irzan : 그 목록을 소포에 쓰는 방법을 의미합니까? 이 경우 http://developer.android.com/reference/android/os/Parcel.html#writeList(java.util.List) 메소드를 사용할 수 있습니다. –

+0

구체적인 예를 들어 주시겠습니까? 고마워요 :) – hectichavana

관련 문제