How to put a List in intent

后端 未结 5 1906
无人共我
无人共我 2020-11-29 11:28

I have a List in one of my activities and need to pass it to the next activity.

private List selectedData;  

I tried putting

5条回答
  •  执笔经年
    2020-11-29 12:02

    This is what worked for me.

    //first create the list to put objects
    private ArrayList itemsList = new ArrayList<>();
    
    //on the sender activity
         //add items to list where necessary also make sure the Class model ItemCreate implements Serializable
         itemsList.add(theInstanceOfItemCreates);
    
            Intent goToActivity = new Intent(MainActivity.this, SecondActivity.class);
                            goToActivity.putExtra("ITEMS", itemsList);
                            startActivity(goToActivity);
    
        //then on second activity
        Intent i = getIntent();
                receivedItemsList = (ArrayList) i.getSerializableExtra("ITEMS");
                Log.d("Print Items Count", receivedItemsList.size()+"");
                for (Received item:
                     receivedItemList) {
                    Log.d("Print Item name: ", item.getName() + "");
            }
    

    I hope it works for you too.

提交回复
热议问题