I know you can pass in an array list of String via intent, but what if it's an array list of some object that I defined? Say an array list of Bicyles, how do I do this?
Intent intent = new Intent(getActivity(), StudentResult. class); intent. putExtra("ExtraData", allStudents); startActivity(intent); and in target class to show the objects in ListView();
One option could be letting your custom class implement the Serializable interface and then you can pass object instances in the intent extra using the putExtra(Serializable..) variant of the Intent#putExtra() method.
You can make your objects implement Parcelable and use putParcelableArrayListExtra. Alternatively, you can serialize your objects in some way and put the byte array of your serialized objects.
This is an example. MainActivity sends list of persons to OtherActivity via Intent.
class Person implements Serializable {
    int id;
    String name;
    Person(int i, String s) {
        id = i;
        name = s;
    }
}
public class TestAndroidActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        ArrayList<Person> list = new ArrayList<Person>();
        list.add(new Person(1, "Tom"));
        list.add(new Person(5, "John"));
        Intent intent = new Intent(this, OtherActitity.class);
        intent.putExtra("list", list);
        startActivity(intent);
OtherActivity.java
import java.util.ArrayList;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Toast;
public class OtherActitity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.other);
        Intent i = getIntent();
        ArrayList<Person> list = (ArrayList<Person>) i
                .getSerializableExtra("list");
        Toast.makeText(this, list.get(1).name, Toast.LENGTH_LONG).show();
    }
}
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With