【问题标题】:Passing ArrayList of objects through intent - Java (Android)通过意图传递对象的ArrayList - Java(Android)
【发布时间】:2012-11-22 11:02:34
【问题描述】:

我正在尝试通过意图传递对象的 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,正在接收,我可以使用它们。 ArrayList 没有通过。 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了吗?
  • 不...这就是为什么他要投...(ArrayList&lt;? extends Parcelable&gt;) queries); :) 这当然无济于事
  • 我们能看到你的查询类型 ArrayList 的实际样子吗?我的意思是班级...
  • @Selvin,但他没有得到奇怪的类转换异常......
  • java 泛型 ...类似的代码不会在 C# 中编译,但在 java 中 ...ArrayList&lt;Object&gt; a = null; ArrayList&lt;? extends Parcelable&gt; b = (ArrayList&lt;? extends Parcelable&gt;) a; 有效 ...只有警告 Unchecked cast from ArrayList&lt;Object&gt; to ArrayList&lt;? extends Parcelable&gt; ....在运行时在 java 中你不需要'不知道泛型类型

标签: java android android-intent arraylist


【解决方案1】:

您必须更改您的 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 数组的方式非常完美。

【讨论】:

  • 感谢山姆的详细回答!我没有给你我所有的代码,所以我可能会让你偏离正轨。我用我所有的课程编辑了我的开场白。现在有了这个,我应该实现 Parcelable 并在 QuestionView 或 Question 中添加所有其他方法吗?
  • 你只想传递一个 ArrayList of Questions,所以只有 Question 类应该实现 Parcelable
  • 天哪,它有效!这是我在这两个月里工作的最远的一次。今天的重大里程碑!再次感谢山姆。 :)
  • @Sam 如何打包位图?
【解决方案2】:

为了能够将 ArrayList 作为 Intent 的一部分提供,您的类型必须实现 Parcelable。从您必须将列表转换为(ArrayList&lt;? extends Parcelable&gt;) 的事实来看,您没有这样做。如果你有,你可以简单地拥有:

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

【讨论】:

  • 没有必要实现 Parcelable,但使用 Serializable 会带来性能损失,因此您实际上应该使用 Parcelable。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-07
  • 1970-01-01
  • 2018-09-11
  • 1970-01-01
相关资源
最近更新 更多