2012-12-04 3 views
11

의도를 통해 객체의 ArrayList를 전달하려고 시도하지만 작동하지 않습니다. 여기에 내가 무엇을 가지고 :인 텐트를 통해 객체의 ArrayList 전달 - Java (Android)

public class QuestionView extends Activity { 

    //variables and other methods... 

    public void endQuiz() { 
     Intent intent = new Intent(QuestionView.this, Results.class); 
     intent.putExtra("correctAnswers", correctAnswers); 
     intent.putExtra("wrongAnswers", wrongAnswers); 
     intent.putParcelableArrayListExtra("queries", queries); 
     startActivity(intent); 
    } 
} 

텐트는 여기에 수신됩니다

public class Results extends Activity { 

    int cAnswers; 
    int wAnswers; 
    ArrayList<Question> qs; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.resultsmain); 

     cAnswers = getIntent().getIntExtra("correctAnswers", -1); 
     wAnswers = getIntent().getIntExtra("wrongAnswers", -1); 

     qs = getIntent().getParcelableArrayListExtra("queries"); 

        //more code... 
    } 
} 

두의 int는 correctAnswer 및 wrongAnswers가 수신되고 있으며, 내가 그들을 사용할 수 있습니다. ArrrayList가 나오지 않습니다. endQuiz() 메소드에는 오류가 없지만 'qs = getIntent(). getParcelableArrayListExtra ("queries");' 오류가 발생하고 "Bound Mismatch"라고 말하고 있습니다.

이 문제에 대한 도움이 필요하십니까?

질문 클래스 :

public class Question { 
    String a1; 
    String a2; 
    String a3; 
    String a4; 
    int correctAnswer; 
    String query; 
    int selectedAnswer; 
    boolean correctness; 

    public Question() { 
    } 

    public Question(String a1, String a2, String a3, String a4, int correctAnswer, String query, int selectedAnswer, boolean correctness) { 
     this.a1 = a1; 
     this.a2 = a2; 
     this.a3 = a3; 
     this.a4 = a4; 
     this.correctAnswer = correctAnswer; 
     this.query = query; 
     this.selectedAnswer = selectedAnswer; 
     this.correctness = correctness; 
    } 
    } 
+1

'Question' 클래스는 [Parcelable] (http://developer.android.com/reference/android/os/Parcelable.html)을 구현합니까? – wsanville

+3

아니요 ... 왜 그가 그것을 캐스팅하는지 ... (ArrayList ) queries :':) 확실히 도움이되지 않습니다 – Selvin

+0

ArrayList가 실제로 보이는 쿼리 유형을 확인할 수 있습니까? 나는 클래스를 의미합니다 ... – LuxuryMode

답변

25

. Parcelable 인터페이스는 처음에 혼란 스러울 수 있지만 거기에 매달려 있습니다.

당신이 집중해야 두 Parcelable 방법이 있습니다 :

소포 객체로 클래스를 변환
  • writeToParcel().
  • Question(Parcel in) Parcel 개체를 사용 가능한 클래스 인스턴스로 다시 변환합니다.

&은 내가 채점 한 다른 Parcelable 정보를 붙여 넣을 수 있습니다. 당신이 당신의 의도 엑스트라로 queries를 넣어하는 방법

public class Question implements Parcelable { 
    String a1; 
    String a2; 
    ... 

    public Question(String a1, String a1) { 
     this.a1 = a1; 
     this.a2 = a2; 
    } 

    // Your existing methods go here. (There is no need for me to re-write them.) 

    // The following methods that are required for using Parcelable 
    private Question(Parcel in) { 
     // This order must match the order in writeToParcel() 
     a1 = in.readString(); 
     a2 = in.readString(); 
     // Continue doing this for the rest of your member data 
    } 

    public void writeToParcel(Parcel out, int flags) { 
     // Again this order must match the Question(Parcel) constructor 
     out.writeString(a1); 
     out.writeString(a2); 
     // Again continue doing this for the rest of your member data 
    } 

    // Just cut and paste this for now 
    public int describeContents() { 
     return 0; 
    } 

    // Just cut and paste this for now 
    public static final Parcelable.Creator<Question> CREATOR = new Parcelable.Creator<Question>() { 
     public Question createFromParcel(Parcel in) { 
      return new Question(in); 
     } 

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

지금 변경 : 난 단지 당신의 질문 클래스의 일부를 사용합니다 간단하게하기 위해서

.

intent.putParcelableArrayListExtra("queries", queries); 

Parcelable 배열을 읽는 방법은 그대로입니다.

+0

그 상세한 답변을 주셔서 감사합니다 샘! 나는 당신에게 모든 코드를주지 않았으므로 나는 당신을 코스에서 쫓아 냈을지도 모른다. 나는 나의 모든 수업과 함께 나의 오프닝 포스트를 편집했다. 이제는 Parcelable을 구현하고 QuestionView 또는 Question에 다른 모든 메서드를 추가해야합니까? – Matt

+0

질문의 ArrayList 만 전달하면됩니다. Question 클래스 만 Parcelable을 구현해야합니다. – Sam

+0

아, 작동합니다! 이것은 제가 2 개월 동안 가장 노력한 결과입니다. 오늘 엄청난 이정표! 샘 고마워. :) – Matt

7

위하여는 의도의 일환으로 ArrayList의를 제공 할 수 있도록, 당신의 유형은 Parcelable를 구현해야합니다. 목록을 (ArrayList<? extends Parcelable>)으로 전송해야한다는 판단에이 작업을 수행하지 않았습니다. 당신이 있다면, 당신은 단순히 수 : 당신은 실제로 Parcelable을 구현하기 위해 Question 클래스를 변경해야합니다

class Foo implements Parcelable { 
//implementation here 
} 
ArrayList<Foo> foos = new ArrayList<Foo>(); 
new Intent().putParcelableArrayListExtra("foos", foos); //no need to cast 
+0

Parcelable을 구현할 필요는 없지만 Serializable을 사용하면 성능 저하가 발생하므로 실제로 Parcelable을 사용해야합니다. –

관련 문제