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 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.
send Arraylist by Intent
The first part to understand is that you pass information from Activity A to Activity B using an Intent
object, inside which you can put "extras". The complete listing of what you can put inside an Intent
is available here: https://developer.android.com/reference/android/content/Intent.html (see the various putExtra()
methods, as well as the putFooExtra()
methods below).
Since you are trying to pass an ArrayList<Song>
, you have two options.
The first, and the best, is to use putParcelableArrayListExtra()
. To use this, the Song
class must implement the Parcelable
interface. If you control the source code of Song
, implementing Parcelable
is relatively easy. Your code might look like this:
Intent intent = new Intent(ActivityA.this, ActivityB.class);
intent.putParcelableArrayListExtra("songs", songs);
The second is to use the version of putExtra()
that accepts a Serializable
object. You should only use this option when you do not control the source code of Song
, and therefore cannot implement Parcelable
. Your code might look like this:
Intent intent = new Intent(ActivityA.this, ActivityB.class);
intent.putSerializableExtra("songs", songs);
So that's how you put the data into the Intent
in Activity A. How do you get the data out of the Intent
in Activity B?
It depends on which option you selected above. If you chose the first, you will write something that looks like this:
List<Song> mySongs = getIntent().getParcelableArrayListExtra("songs");
If you chose the second, you will write something that looks like this:
List<Song> mySongs = (List<Song>) getIntent().getSerializableExtra("songs");
The advantage of the first technique is that it is faster (in terms of your app's performance for the user) and it takes up less space (in terms of the size of the data you're passing around).
Passing ArrayList through Intent
In your receiving intent you need to do:
Intent i = getIntent();
stock_list = i.getStringArrayListExtra("stock_list");
The way you have it you've just created a new empty intent without any extras.
If you only have a single extra you can condense this down to:
stock_list = getIntent().getStringArrayListExtra("stock_list");
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");
Sending arraylist object to another activity in android
Make Planet
Parcelable using http://parcelabler.com/
and then send using
intent.putExtra("QuestionListExtra", apl);
and in Proceed
activity
ArrayList<Planet> list = getIntent().getParcelableArrayListExtra("QuestionListExtra");
Related Topics
Recommended Way/Order to Read Data from a Webservice, Parse That Data and Insert It in a SQLite Db
Sync Data Between Android App and Webserver
What Is an Android Pendingintent
How to Programmatically Open the Permission Screen for a Specific App on Android 6.0 (Marshmallow)
What to Use Instead of "Addpreferencesfromresource" in a Preferenceactivity
How to Make a Dotted/Dashed Line in Android
How to Debug JavaScript on Android
How to Hide a Menu Item in the Actionbar
Call Activity Method from Adapter
Android: Bitmaps Loaded from Gallery Are Rotated in Imageview
How to Import Existing Android Project into Eclipse
How to Decompile an Android .Apk File
How to Play Ringtone/Alarm Sound in Android
How to Change the Color of Alertdialog Title and the Color of the Line Under It
How to Pass the Values from One Activity to Previous Activity
Counting Chars in Edittext Changed Listener
Failed to Load Map. Error Contacting Google Servers. This Is Probably an Authentication Issue