This is default featured slide 1 title

Go to Blogger edit html and find these sentences.Now replace these sentences with your own descriptions.This theme is Bloggerized by Lasantha Bandara - Premiumbloggertemplates.com.

This is default featured slide 2 title

Go to Blogger edit html and find these sentences.Now replace these sentences with your own descriptions.This theme is Bloggerized by Lasantha Bandara - Premiumbloggertemplates.com.

This is default featured slide 3 title

Go to Blogger edit html and find these sentences.Now replace these sentences with your own descriptions.This theme is Bloggerized by Lasantha Bandara - Premiumbloggertemplates.com.

This is default featured slide 4 title

Go to Blogger edit html and find these sentences.Now replace these sentences with your own descriptions.This theme is Bloggerized by Lasantha Bandara - Premiumbloggertemplates.com.

This is default featured slide 5 title

Go to Blogger edit html and find these sentences.Now replace these sentences with your own descriptions.This theme is Bloggerized by Lasantha Bandara - Premiumbloggertemplates.com.

Showing posts with label Sending ArrayList of Objects as extras from one Activity to Other. Show all posts
Showing posts with label Sending ArrayList of Objects as extras from one Activity to Other. Show all posts

Monday, 21 January 2013

Sending ArrayList of Objects as extras from one Activity to Other

Question.java
public class Question implements Serializable {

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

}
In your Source Activity, use this :

List<Question> mQuestionList = new ArrayList<Question>;
int[] ops = {1,2,3,4,5};
int[] choices = {12,45,23,16};
mQuestionList.add(new Question(ops, choices));

ops1 = {11,22,33,44,55};
choices1 = {122,453,423,516};
mQuestionList.add(new Question(ops1, choices1));
Intent intent = new Intent(SourceActivity.this, TargetActivity.class);
intent.putExtra("QuestionListExtra", ArrayList<Question>mQuestionList);
In your Target Activity, use this :
List<Question> questions = new ArrayList<Question>();
 questions = (ArrayList<Question>)getIntent().getSerializableExtra("QuestionListExtra");

Sending ArrayList of Objects as extras from one Activity to Other to Android