首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >通过intent传递对象的ArrayList - Java (安卓)

通过intent传递对象的ArrayList - Java (安卓)
EN

Stack Overflow用户
提问于 2012-12-05 02:20:37
回答 2查看 21.5K关注 0票数 13

我试图通过一个意图传递一个对象的ArrayList,但是不能让它工作。这就是我所拥有的:

代码语言:javascript
复制
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);
    }
}

意向在这里被接收:

代码语言:javascript
复制
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");‘抛出了一个错误,并显示“绑定不匹配”。

在这方面的任何帮助都是非常感谢的!

问题类:

代码语言:javascript
复制
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;
    }
    }
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-12-05 02:46:59

您必须更改您的Question类以实际实现Parcelable。Parcelable接口一开始可能会让人感到困惑...但要坚持住。

有两个Parcelable方法是您应该关注的:

  • writeToParcel()将您的类转换为宗地object.
  • Question(Parcel in),后者将宗地对象转换回您的类的可用实例。

您可以安全地剪切和粘贴我标记的其他Parcelable信息。

为了简单起见,我将只使用您的问题类的一部分:

代码语言:javascript
复制
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放入意图附加组件的方式。

代码语言:javascript
复制
intent.putParcelableArrayListExtra("queries", queries);

您读取Parcelable数组的方式是完美的。

票数 27
EN

Stack Overflow用户

发布于 2012-12-05 02:30:10

为了能够将ArrayList作为Intent的一部分交付,您的类型必须实现Parcelable。从你必须将你的列表传递给(ArrayList<? extends Parcelable>)的事实来看,你没有这样做。如果你有,你可以简单地拥有:

代码语言:javascript
复制
class Foo implements Parcelable {
//implementation here
}
ArrayList<Foo> foos = new ArrayList<Foo>();
new Intent().putParcelableArrayListExtra("foos", foos); //no need to cast
票数 7
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13709362

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档