How to pass ArrayListCustomeObject 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 ArrayList with objects to new Activity?
Complex types may be passed by means of Parcelable. An example is in this question:
Help with passing ArrayList and parcelable Activity
Passing ArrayListObject back to main Activity (Android)
For the easiest way, your Song class need implement Serializable.
Bundle bundle = new Bundle();
bundle.putSerializable("newPlaylist", newPlaylist);
intent.putExtras(bundle);
in other activity:
Intent intent = this.getIntent();
Bundle bundle = intent.getExtras();
ArrayList<Song> newPlaylist=
(ArrayList<Song>)bundle.getSerializable("newPlaylist");
Passing arraylist of objects between activities
You should make your Song
class implement Parcelable
.
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
Related Topics
How to Get Minutes Difference Between Two Time in Android
How to Display an Image in Full Screen on Click in the Imageview
Where Is Android_Sdk_Root and How to Set It.
Check Programmatically If App Is Installed on the Device
Android Blur View (Blur Background Behind the View)
How to Get Last Record from Sqlite
Android Check Null or Empty String in Android
App Crashes (Sometimes) With Fatal Signal 11 (Sigsegv), Code 1
How to Refresh a Previous Activity After the Back Button Is Pressed
How to Measure Distance to Object With Camera
How to Connect Localhost in the Android Emulator
How to Completely Rename an Android Application
Navigate from One Fragment to Another on Click of a Button
Android: Mkdirs()/Mkdir() on External Storage Returns False
Android Fastboot Waiting for Devices
Android Edittext Text Control - How to Disable Possibility to Insert Gif from Google Keyboard