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?
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.
Comments