簡體   English   中英

通過intent傳遞對象的ArrayList - Java(Android)

[英]Passing ArrayList of objects through intent - Java (Android)

我試圖通過意圖傳遞對象的ArrayList但無法讓它工作。 這是我有的:

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

正在接收兩個整數,correctAnswer和wrongAnswers,我可以使用它們。 ArrrayList沒有通過。 在endQuiz()方法中沒有錯誤,但'qs = getIntent()。getParcelableArrayListExtra(“queries”);' 拋出錯誤並說“綁定不匹配”。

對此有任何幫助表示贊賞!

問題類:

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

您必須更改Question類以實際實現Parcelable。 Parcelable界面起初可能會讓人感到困惑......但請掛在那里。

您應該關注兩種Parcelable方法:

  • writeToParcel()將您的類轉換為Parcel對象。
  • Question(Parcel in)將Parcel對象轉換回類的可用實例。

您可以安全地剪切和粘貼我標記的其他Parcelable信息。

為簡單起見,我將僅使用您的問題類的一部分:

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附加組件的方式。

intent.putParcelableArrayListExtra("queries", queries);

您閱讀Parcelable陣列的方式非常完美。

為了能夠將ArrayList作為Intent的一部分提供,您的類型必須實現Parcelable。 從你必須將List轉換為(ArrayList<? extends Parcelable>)的事實來判斷,你沒有這樣做。 如果你有,你可以簡單地:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM