通过意图传递对象的ArrayList - 爪哇(安卓)爪哇、意图、对象、ArrayList

2023-09-04 05:10:42 作者:无奈我和你 写不出结局つ

我想通过一个意图传递对象的ArrayList,但不能让它开始工作。以下是我有:

 公共类QuestionView延伸活动{

    //变量等方法...

    公共无效endQuiz(){
        意向意图=新的意图(QuestionView.this,Results.class);
        intent.putExtra(correctAnswers​​,correctAnswers);
        intent.putExtra(wrongAnswers​​,wrongAnswers);
        intent.putParcelableArrayListExtra(查询,查询);
        startActivity(意向);
    }
}
 

意图正在接收的位置:

 公共类结果扩展活动{

    INT cAnswers;
    INT wAnswers;
    ArrayList的<问题> QS;

    @覆盖
    公共无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.resultsmain);

        cAnswers = getIntent()getIntExtra(correctAnswers​​,-1)。
        wAnswers = getIntent()getIntExtra(wrongAnswers​​,-1)。

        QS = getIntent()getParcelableArrayListExtra(查询)。

                    //更多code ...
    }
}
 

这两个整数,correctAnswer和wrongAnswers,正在接收,我可以使用它们。该ArrrayList不来通过。在没有错误的endQuiz()方法,但QS = getIntent()getParcelableArrayListExtra(查询)。抛出一个错误,并说三寸不匹配。

任何帮助是AP preciated!

问题类:

 公共类问题{
    字符串A1;
    字符串A2;
    字符串A3;
    字符串A4;
    INT correctAnswer;
    查询字符串;
    INT selectedAnswer;
    布尔正确性;

    公众质疑(){
    }

    公众质疑(字符串A1,A2的字符串,字符串A3,A4的字符串,INT correctAnswer,查询字符串,INT selectedAnswer,布尔正确性){
        this.a1 = A1;
        this.a2 = A2;
        this.a3 = A3;
        this.a4 = A4;
        this.correctAnswer = correctAnswer;
        this.query =查询;
        this.selectedAnswer = selectedAnswer;
        this.correctness =正确性;
    }
    }
 
Android通过Intent使用Bundle传递对象详细介绍 其它代码类资源 CSDN下载

解决方案

您必须更改问题类实际执行Parcelable。该Parcelable接口可能会造成混淆在第一......但挂在那里。

有两种Parcelable方法,您应把重点放在:

writeToParcel()它把你的类变成一个包对象。 问题(包裹在)它把一个包裹对象返回到您的类的可用实例。

您可以安全地剪切和放大器;粘贴标志着我其他Parcelable信息。

有关简单起见,我将只使用你的问题类的一部分:

 公共类问题实现Parcelable {
    字符串A1;
    字符串A2;
    ...

    公众质疑(A1字符串,字符串A1){
        this.a1 = A1;
        this.a2 = A2;
    }

    //你现有的方法去这里。 (有没有需要我重新写。)

    //所需要的使用Parcelable以下方法
    私人问题(宗地中){
        //这个命令必须writeToParcel的顺序匹配()
        A1 = in.readString();
        A2 = in.readString();
        //继续这样做,你的会员数据的其余部分
    }

    公共无效writeToParcel(包裹出来,诠释标志){
        //同样,这顺序必须匹配问题(包裹)构造函数
        out.writeString(a1)中;
        out.writeString(a2)的;
        //再继续这样做,你的会员数据的其余部分
    }

    //只要剪切和粘贴现在
    公众诠释describeContents(){
        返回0;
    }

    //只要剪切和粘贴现在
    公共静态最终Parcelable.Creator<问题> CREATOR =新Parcelable.Creator<问题>(){
        公众质疑createFromParcel(包裹中){
            返回新的问题(在);
        }

        公众质疑[] newArray(INT尺寸){
            返回新课题【尺寸】;
        }
    };
}
 

现在改变你怎么把查询到你的意图演员。

  intent.putParcelableArrayListExtra(查询,查询);
 

这就是你阅读Parcelable阵列是完美的,因为它是。

I am trying to pass an ArrayList of objects through an intent but cannot get it to work. Here is what I have:

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 are being received here:

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

The two ints, correctAnswer and wrongAnswers, are being received and I can use them. The ArrrayList is not coming through. No errors on in the endQuiz() method but the 'qs = getIntent().getParcelableArrayListExtra("queries");' is throwing an error and saying "Bound Mismatch."

Any help on this is appreciated!

Question class:

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

解决方案

You must change your Question class to actually implement Parcelable. The Parcelable interface can be confusing at first... but hang in there.

There are two Parcelable methods that you should focus on:

writeToParcel() which converts your class into a Parcel object. Question(Parcel in) which converts a Parcel object back into usable instance of your class.

You can safely cut & paste the other Parcelable information that I marked.

For the sake of simplicity I will only use part of your Question class:

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

Now change how you put queries into your Intent extras.

intent.putParcelableArrayListExtra("queries", queries);

The way you read the Parcelable array is perfect as it is.