2012-12-04 9 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... 
    } 
} 

2つの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 <?extends Parcelable>); ':)確かに助けにならないでしょう – Selvin

+0

ArrayListの実際の型はどういうものが見えますか?私はクラスを意味します... – LuxuryMode

答えて

25

あなたが実際にParcelableを実装するために、あなたのQuestionクラスを変更する必要があります。 Parcelableインターフェイスは最初は混乱するかもしれませんが、そこにハングアップします。

あなたが焦点を当てるべき2つのParcelableの方法があります。

小包オブジェクトにあなたのクラスを変換
  • writeToParcel()
  • Question(Parcel in) Parcelオブジェクトをクラスの使用可能なインスタンスに変換します。

&は、マークした他のパーセル可能な情報を貼り付けても安全です。

簡単にするために、私は、あなたの質問クラスの一部を使用します。

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

は今、あなたはあなたのテントエキストラにqueriesを置く方法を変更。

intent.putParcelableArrayListExtra("queries", queries); 

あなたがParcelable配列を読み込む方法はそのままです。

+0

その詳細なお返事ありがとうSam!私はあなたにすべてのコードを渡したわけではありませんので、私はあなたをコースから捨てたかもしれません。私は全てのクラスでオープニングポストを編集しました。それでは、Parcelableを実装して、QuestionViewまたはQuestionの他のメソッドをすべて追加する必要がありますか? – Matt

+0

質問のArrayListだけを渡したいので、QuestionクラスだけがParcelableを実装する必要があります – Sam

+0

ああ、それは動作します!これは私が2ヶ月で一番遠いことです。私はこれに関して取り組んでいます。今日の大きなマイルストーン!もう一度サムに感謝します。 :) – Matt

7

テントの一部としてのArrayListを提供することができるようにするために、あなたのタイプはParcelableを実装する必要があります。 (ArrayList<? extends Parcelable>)にあなたのリストをキャストしなければならなかったという事実から判断すると、あなたはこれをしませんでした。あなたが持っていた場合、あなたは単にかもしれない:

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を使用する必要があります。 –

関連する問題