通过意图传递对象的数组列表 - Java (Android)
2022-09-04 06:16:54
我正在尝试通过意图传递对象的 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和wrightAnswers,正在被接收,我可以使用它们。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;
}
}