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");
how to pass Array list Object to another activity
Please write ShowProducts implements Serializable
in your model class.
This will resolve the issue.
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");
Passing a List 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");
Please note that serialization can cause performance issues: it takes time, and a lot of objects will be allocated (and thus, have to be garbage collected).
How to pass ArrayList File object from one Activity to another Activity using Intent?
If your File
is serializable,then you can pass the ArrayList
like this:
In the sending Activity:
ArrayList<File> documents= new ArrayList<File>();
intent.putExtra("documents", documents);
In the receiving Activity:
ArrayList<File> documents = (ArrayList<File>)getIntent().getSerializableExtra("documents");
Pass ArrayList from to Activity
No need to create new class DataWrapper.java. Because ArrayList is serializable.
Non-Activiy class
public void showActivity() {
Intent intent = new Intent(request, ActivityKorak.class);
intent.putExtra("title", title);
intent.putExtra("data", fields);
request.startActivity(intent);
}
ActivityKorak.class->onCreate()
Intent intent = getIntent();
String title = intent.getStringExtra("title");
DataWrapper dw = (DataWrapper) intent.getSerializableExtra("data");
ArrayList<CustomInput> fields = dw.getFields();
Related Topics
Android Spanned, Spannedstring, Spannable, Spannablestring and Charsequence
Force Update of an Android App When a New Version Is Available
Recyclerview Inside Scrollview Not Scrolling Smoothly
How to Clear an Imageview in Android
Android Onclick in Xml VS. Onclicklistener
How to Make a Phone Call Programmatically
How to Lock Orientation During Runtime
How to Make a View with Rounded Corners
Pick Any Kind of File via an Intent in Android
Mediaplayer Stutters at Start of Mp3 Playback
Read & Writing Arrays of Parcelable Objects
Android: Android.Content.Res.Resources$Notfoundexception: String Resource Id #0X5
Location Needs to Be Enabled for Bluetooth Low Energy Scanning on Android 6.0
Device Not Detected in Eclipse When Connected with Usb Cable
Android - Getting Audio to Play Through Earpiece