Passing ArrayList<Person> to new activity

user3906716

I want to pass ArrayList to new activity, in the new activity I will load the list with ArrayAdapter.

But I can not transfer the object to the new activity I am getting null on the new activity.

I have read that I need to implement serialization to the Person class... Is this the only way?

Here is my code:

AsyncTask onPostExecute I am getting the array.

@Override
    protected void onPostExecute(ArrayList<Person> personArrayList){

        Intent intent = new Intent(activity, ResultsActivity.class);
        intent.putExtra("personArrayList",personArrayList);
        activity.startActivity(intent);

    }

Sending it with putExtra.

Here is the Activity which suppose to receive the array.

public class ResultsActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_results);
        Intent intent = getIntent();
        ArrayList<Person> personArrayList = (ArrayList<Person>) intent.getSerializableExtra("personArrayList");

        PeopleAdapter adapter = new PeopleAdapter(this, personArrayList);

        // Find the {@link ListView} object in the view hierarchy of the {@link Activity}.
        // There should be a {@link ListView} with the view ID called list, which is declared in the
        // activity_results.xml layout file.

        ListView listView = (ListView) findViewById(R.id.list);


        // Make the {@link ListView} use the {@link WordAdapter} we created above, so that the
        // {@link ListView} will display list items for each {@link Person} in the list.
        listView.setAdapter(adapter);
    }
}

So on the receiving end the ArrayList is empty. Ideas?

Husayn Hakeem

You transfer this ArrayList by making your class Person implement the Parcelable interface. Once that's done all you need to do is write:

@Override
protected void onPostExecute(ArrayList<Person> personArrayList){

    Intent intent = new Intent(activity, ResultsActivity.class);
    intent.putParcelableArrayListExtra("personArrayList",personArrayList);
    activity.startActivity(intent);

}

In your ResultsActivity class, you get this array by writing intent.getParcelableArrayListExtra("personArrayList")

Hope this helps.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Passing an ArrayList of other Arrays to an Activity

Class to Activity passing ArrayList values

String Extra not passing to new Activity

Passing ArrayList<Object> back to main Activity (Android)

Android passing ArrayList<Model> to Fragment from Activity

ArrayIndexOutOfBoundsException / passing arraylist from alertdialog to activity

Passing ArrayList from Fragment class to Activity

Passing and retrieving MutableList or ArrayList from Activity A to B

Passing new ArrayList vs Passing ArrayList as a parameter in Function(Java)

How to start a new activity passing an image uri

Passing data from list fragment to new activity

How to pass an ArrayList and an integer to a new activity (Android)

Remove a person from arrayList of person

passing arraylist item of tabfragment recyclerview to other activity recyclerview

Removing a person from arrayList

Firebase data retrieve and passing it from fragment to new activity not working

Passing data from json object listview to new activity

App crashed after passing ListView position int to new activity

Passing ID from adapter to new activity but getting null

Java passing/displaying a Int value to a new activity on click

Method addFriend(Person) is undefined for the type ArrayList<Person>

Why am I getting a null pointer exception when passing an ArrayList of strings from one activity to another?

Unchecked Cast error while passing an arraylist from one activity to another using intent

Activity not passing intent to onActivityResult()

Passing intents to next activity

Passing reference to activity

Passing values between activity

Passing Activity as Parameter in Notification

Passing multiple strings to a new activity and displaying them in a list view. Java, Android SDK