Passing Arraylist Through Intent

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 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 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");

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).

pass Arraylist through intent

HashMap extends Serializable interface, so you can pass an ArrayList of Serializable objects:

ArrayList<HashMap<String, String>> taskItems = new ArrayList<>();
in.putExtra("taskItems", taskItems);

and fetch it like:

ArrayList<HashMap<String, String>> taskItems = (ArrayList<HashMap<String, String>>) in
.getSerializableExtra("taskItems");

Arraylist Pass from fragment to activity

Hope this will help you!
Paste the following code in your activity.

ArrayList<Object> imagearraylist = (ArrayList<Object>) getIntent().getSerializableExtra("Arraylist");

Reference: How to pass ArrayList<CustomeObject> from one activity to another?



Related Topics



Leave a reply



Submit