2012-12-04 14 views
11

Próbuję przekazać ArrayList obiektów przez intencję, ale nie mogę go uruchomić. Oto co mam:Mijanie ArrayList obiektów przez intencję - 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); 
    } 
} 

Intents są odbierane tutaj:

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... 
    } 
} 

Dwa ints, correctAnswer i wrongAnswers, są odbierane i mogę z nich korzystać. ArrrayList nie nadchodzi. Brak błędów w metodzie endQuiz(), ale "qs = getIntent(). GetParcelableArrayListExtra (" zapytania "); rzuca błąd i mówi "Bound Mismatch".

Każda pomoc w tym zakresie jest doceniana!

klasa Pytanie:

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

Czy twoja klasa "Pytanie" implementuje [Parcelable] (http://developer.android.com/reference/android/os/Parcelable.html)? – wsanville

+3

nie ... to dlatego go rzuca ... '(ArrayList ) zapytania);' :) co z pewnością nie pomoże – Selvin

+0

Czy możemy zobaczyć, jak wygląda typ zapytań ArrayList? Mam na myśli klasę ... – LuxuryMode

Odpowiedz

25

Musisz zmienić swoją klasę Question rzeczywiście wdrożyć Parcelable. Interfejs Parcelable może początkowo być mylący ... ale w nim wisieć.

Istnieją dwa Parcelable metody, które należy zwrócić uwagę na:

  • writeToParcel() który przekształca swoją klasę do obiektu działki.
  • Question(Parcel in) który przekształca obiekt działki z powrotem w użyteczną instancję klasy.

Możesz bezpiecznie wyciąć & wkleić inne informacje Parcelable, które zaznaczyłem.

Dla uproszczenia będę używać tylko część swojej klasie pytanie:

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]; 
     } 
    }; 
} 

teraz zmienić sposób umieścić queries na intencję dodatkami.

Sposób, w jaki czytasz Parcelable array, jest idealny, jak jest.

+0

Dziękuję za tę szczegółową odpowiedź Sam! Nie podałem ci całego kodu, więc mogłem zrzucić cię z kursu. Redagowałem swój pierwszy post ze wszystkich moich zajęć. A teraz, czy powinienem wdrożyć Parcelable i dodać wszystkie inne metody w QuestionView lub Question? – Matt

+0

Chcesz przekazać tylko ArrayList of Questions, więc tylko klasa Question powinna implementować Parcelable – Sam

+0

Oh man it works! To jest najdalszy okres, w którym pracowałem przez 2 miesiące. Ogromny kamień milowy już dziś! Jeszcze raz dziękuję Sam. :) – Matt

7

Aby móc dostarczać ArrayList jako część intencyjny, Twój typ musi wdrożyć Parcelable. Sądząc po tym, że musiałeś rzucić listę na (ArrayList<? extends Parcelable>), nie zrobiłeś tego. Jeśli miał, można po prostu:

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

Nie jest konieczne wdrażanie Parceli, ale używanie Serializable do tego wymaga kary za wydajność, więc powinieneś używać Parcelable. –