I\'m trying to pass an ArrayList of Person Objects from the MainActivity to SecondActivity to print the details of the person in a custom listView adapter.
The applicati
You should use another function: intent.putParcelableArrayListExtra(String name, ArrayList<? extends Parcelable> value) Google site in main Activity, then intent.getParcelableArrayListExtra to get in second Activity.
Hope this help.
ArrayList does not implements Parcelable, but it implements Serializable, so you can't use getParcelableExtra to receive the data, you must use getSerializableExtra instead.
ArrayList<Person> person = (ArrayList<Person>) i.getSerializableExtra("personObject");
Person class must implements Serializable too, here is the code example:
Person implements Serializable{
private static final long serialVersionUID = 0L;
String id;
String name;
}
Update:
Another solution: use putParcelableArrayListExtra and getParcelableArrayListExtra.
First activity:
ArrayList<Person> list = new ArrayList<Person>();
public void onButtonClick() {
Intent intent = new Intent(getApplicationContext(), SecondActivity.class);
intent.putParcelableArrayListExtra("personObject", list);
startActivity(intent);
}
Second Activity:
ArrayList<Person> person = (ArrayList<Person>) i.getParcelableArrayListExtra("personObject");
Note: Parcelable is faster than Serializable, so the second solution is better if you want to pass a lot of data.
1.Create FirstActivity with one button
package com.appkart.sdcardconnection;
import java.util.ArrayList;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class FirstActivity extends ActionBarActivity {
private Button btn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.first_activity);
btn = (Button) findViewById(R.id.btn);
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(FirstActivity.this, SecondActivity.class);
ArrayList<Person> personList = new ArrayList<Person>();
personList.add(new Person("10", "Arun"));
personList.add(new Person("20", "Ankit"));
intent.putExtra("person_list", personList);
startActivity(intent);
}
});
}
}
2.xml file
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.appkart.sdcardconnection.FirstActivity" >
<Button
android:id="@+id/btn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Second Activity" />
</RelativeLayout>
3.Second Activity
package com.appkart.sdcardconnection;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
public class SecondActivity extends Activity {
private static final String TAG = SecondActivity.class.getSimpleName();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second_activity);
List<Person> personList = (ArrayList<Person>) getIntent()
.getSerializableExtra("person_list");
for (Person person : personList) {
Log.d(TAG, "id : " + person.getId());
Log.d(TAG, "name : " + person.getName());
}
}
}
4.Person POJO
package com.appkart.sdcardconnection;
import java.io.Serializable;
public class Person implements Serializable {
private String id;
private String name;
public Person(String id, String name) {
this.id = id;
this.name = name;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
Use Serializable instead Parcelable. According to ArrayList java doc it implement Serializable. Array list java doc