Passing arraylist of objects between activities
You should make your Song
class implement Parcelable
.
How to pass ArrayList of Objects from one to another activity using Intent in android?
It works well,
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>;
mQuestionsList = QuestionBank.getQuestions();
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");
how to pass Array list Object to another activity
Please write ShowProducts implements Serializable
in your model class.
This will resolve the issue.
Passing ArrayList CustomObject Between Activities
This code
Bundle informacion= new Bundle();
informacion.putParcelableArrayList("eventos", ArrayList<Eventos> eventos);
intent.putExtras(informacion);
Should be
Bundle informacion = new Bundle();
ArrayList<Eventos> mas = new ArrayList<Eventos>();
informacion.putSerializable("eventos", mas);
intent.putExtras(informacion);
and Make sure your Eventos structure like a serializable object
private class Eventos implements Serializable {
}
Reading Values
ArrayList<Eventos> madd=getIntent().getSerializableExtra(key);
How to pass ArrayList CustomeObject from one activity to another?
You can pass an ArrayList<E>
the same way, if the E
type is Serializable
.
You would call the putExtra (String name, Serializable value)
of Intent
to store, and getSerializableExtra (String name)
for retrieval.
Example:
ArrayList<String> myList = new ArrayList<String>();
intent.putExtra("mylist", myList);
In the other Activity:
ArrayList<String> myList = (ArrayList<String>) getIntent().getSerializableExtra("mylist");
Passing ArrayList MyObject Between multiple Activities
If you want to send an ArrayList of objects then your class must implement the Parcelable or Serializable interface .
See these good tutorials for sending custom object between Activities
http://androidideasblog.blogspot.in/2010/02/passing-list-of-objects-between.html
http://www.anddev.org/novice-tutorials-f8/simple-tutorial-passing-arraylist-across-activities-t9996.html
Related Topics
Error:Execution Failed for Task ':Android:Transformclassesandresourceswithproguardforrelease'
Error:(6, 0) Gradle Dsl Method Not Found: 'Google()'
Android New Bottom Navigation Bar or Bottomnavigationview
Convert a PDF Page into Bitmap in Android Java
Android Singletask or Singleinstance Launch Mode
How to Add a Textview to a Linearlayout Dynamically in Android
How to Set Background Color of a View
How to Build the Android Sdk with Hidden and Internal APIs Available
Change Text Color of One Word in a Textview
Get the Current Language in Device
Image in Canvas with Touch Events
Android Studio Gradle Build Speed Up
Finish All Activities at a Time
How to Import Existing Android Project into Eclipse
Show Gif File with Glide (Image Loading and Caching Library)