Pass Arraylist of User Defined Objects to Intent Android

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

Android - Passing an ArrayList of Custom Objects to Another Activity

You need to use Parcelable or Serializable interface.

and the pass it with intent

Intent mIntent = new Intent(context, ResultActivity.class);
mIntent.putParcelableArrayListExtra("list", mArraylist);
startActivity(mIntent);

fetch it in ResultActivity

Bundle bundle = getIntent().getExtras();
mArraylist1 = bundle.getParcelableArrayList("list");

Check this thread for reference

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 of objects between activities

You should make your Song class implement Parcelable.

Sending a custom ArrayList through an Intent

Your code for sending the data from the first activity is correct.

For receiving data in the second activity use intent's getSerializableExtra() method, something like:

ArrayList<String[]> data = (ArrayList<String[]>) getIntent().getSerializableExtra(EXTRA_MESSAGE);

How do I pass arraylist of a custom object from one java class to another?

Serializable and Parcelable are not the same thing, although they serve the same purpose. This post contains an example of the Parcelable object.

The pattern employed should be followed for all Parcelable objects you want to create, changing only the writeToParcel and AddValues(Parcel in) methods. These two methods should mirror eachother, if writeToParcel writes an int then a string, the constructor should read an int then a string

Parcelable

public class AddValues implements Parcelable{
private int id;
private String value;

// Constructor
public AddValues (int id, String value){
this.id = id;
this.value= value;
}

// Parcelling part
public AddValues (Parcel in){
this.id = in.readInt();
this.value= in.readString();
}

@Override
public int describeContents() {
return 0;
}

@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeInt(id);
dest.writeString(value);
}

public final Parcelable.Creator CREATOR = new Parcelable.Creator() {
public AddValues createFromParcel(Parcel in) {
return new AddValues(in);
}

public AddValues[] newArray(int size) {
return new AddValues[size];
}
};

}

Adding the list to an Intent extra should be simple now

Put List extra

ArrayList<AddValue> list = new ArrayList<>();
Intent intent = new Intent(BluetoothLeService.this,HomePageFragment.class);
intent.putExtra("arg_key", list);

Get List extra

ArrayList<AddValues> list = (ArrayList<AddValues>) intent.getSerializableExtra("arg_key");

As an aside, you can use the Pair<Integer, String> object instead of creating an AddValues object. This doesn't affect the answer, but might be nice to know.

Passing ArrayListMyObject 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



Leave a reply



Submit