我的 onClick()
方法中的代码中有以下内容
List<Question> mQuestionsList = QuestionBank.getQuestions();
现在我有这行之后的 Intent ,如下:
Intent resultIntent = new Intent(this, ResultActivity.class);
resultIntent.putParcelableArrayListExtra("QuestionsExtra", (ArrayList<? extends Parcelable>) mQuestionsList);
startActivity(resultIntent);
我不知道如何将 Intent 中的这个问题列表从一个 Activity 传递到另一个 Activity 我的问题课
public class Question {
private int[] operands;
private int[] choices;
private int userAnswerIndex;
public Question(int[] operands, int[] choices) {
this.operands = operands;
this.choices = choices;
this.userAnswerIndex = -1;
}
public int[] getChoices() {
return choices;
}
public void setChoices(int[] choices) {
this.choices = choices;
}
public int[] getOperands() {
return operands;
}
public void setOperands(int[] operands) {
this.operands = operands;
}
public int getUserAnswerIndex() {
return userAnswerIndex;
}
public void setUserAnswerIndex(int userAnswerIndex) {
this.userAnswerIndex = userAnswerIndex;
}
public int getAnswer() {
int answer = 0;
for (int operand : operands) {
answer += operand;
}
return answer;
}
public boolean isCorrect() {
return getAnswer() == choices[this.userAnswerIndex];
}
public boolean hasAnswered() {
return userAnswerIndex != -1;
}
@Override
public String toString() {
StringBuilder builder = new StringBuilder();
// Question
builder.append("Question: ");
for(int operand : operands) {
builder.append(String.format("%d ", operand));
}
builder.append(System.getProperty("line.separator"));
// Choices
int answer = getAnswer();
for (int choice : choices) {
if (choice == answer) {
builder.append(String.format("%d (A) ", choice));
} else {
builder.append(String.format("%d ", choice));
}
}
return builder.toString();
}
}
最佳答案
Activity 之间:为我工作
ArrayList<Object> object = new ArrayList<Object>();
Intent intent = new Intent(Current.class, Transfer.class);
Bundle args = new Bundle();
args.putSerializable("ARRAYLIST",(Serializable)object);
intent.putExtra("BUNDLE",args);
startActivity(intent);
在 Transfer.class 中
Intent intent = getIntent();
Bundle args = intent.getBundleExtra("BUNDLE");
ArrayList<Object> object = (ArrayList<Object>) args.getSerializable("ARRAYLIST");
希望有人能帮上忙。
使用 Parcelable 在 Activity 之间传递数据
这通常在您创建 DataModel 时起作用
例如假设我们有一个 json 类型
{
"bird": [{
"id": 1,
"name": "Chicken"
}, {
"id": 2,
"name": "Eagle"
}]
}
这里的bird是一个List,它包含两个元素
我们将使用 jsonschema2pojo 创建模型
现在我们有了模型类 Name BirdModel 和 Bird BirdModel 由鸟类列表组成 和 Bird 包含名称和 id
进入小鸟类,添加接口(interface)“implements Parcelable”
通过 Alt+Enter 在 android studio 中添加实现方法
注意:会出现一个对话框,说 Add implements method 按回车
按 Alt + Enter 添加 Parcelable 实现
注意:会出现一个对话框,说添加 Parcelable 实现 并再次输入
现在将其传递给 Intent 。
List<Bird> birds = birdModel.getBird();
Intent intent = new Intent(Current.this, Transfer.class);
Bundle bundle = new Bundle();
bundle.putParcelableArrayList("Birds", birds);
intent.putExtras(bundle);
startActivity(intent);
在创建时转移 Activity
List<Bird> challenge = this.getIntent().getExtras().getParcelableArrayList("Birds");
谢谢
如果有任何问题,请告诉我。
关于android - 如何在android中使用Intent将对象的ArrayList从一个 Activity 传递到另一个 Activity ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13601883/