This question has already been answered:
- ArrayList in Bundle 2 replies
How to get ArrayList with an Activity in a Fragment using the Parcelable interface and extract data in a Fragment?
Activity.class
public class MainActivity extends Activity { final String LOG_TAG = "myLogs"; ArrayList<Parcelable> parcelableArrayList; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ArrayList carList = new ArrayList(); carList.add(new Car(1, "Honda", "Black")); carList.add(new Car(2, "Toyota", "Blue")); carList.add(new Car(3, "Suzuki", "Green")); Bundle bundle=new Bundle(); bundle.putParcelableArrayList("name",carList); Fragment fragobj=new MyFragment(); fragobj.setArguments(bundle); getFragmentManager().beginTransaction() .replace(R.id.fmContent, fragobj) .commit(); } Fragragment.class
public class MyFragment extends Fragment{ final String LOG_TAG = "myLogs"; Bundle bundle; TextView text; TextView article; ArrayList<String > arrayList = new ArrayList<String >(); ArrayList<Parcelable> parcelableArrayList; public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment1, container, false); parcelableArrayList = getActivity().getIntent().getParcelableArrayListExtra("name"); if (parcelableArrayList !=null){ Log.d(LOG_TAG, " -------------------------> myBundle contain IS "+arrayList.get(1)); }else { final String LOG_TAG = "myLogs"; // Bundle bundle; Log.d(LOG_TAG, " -------------------------> myBundle contain IS "+"NULL"); } return view; } } In the class MyFragment.java, I get null on the line parcelableArrayList = getActivity (). GetIntent (). GetParcelableArrayListExtra ("Student");
How to organize the code for the transfer type ArrayList?