简体   繁体   中英

How to pass List<Integer> array from one activity to another activity

I have here a array list containing of Integers

static List<Integer> var_Pposition = new ArrayList<>();

I try this like so, But there's error tells

Intent in = new Intent(merchandise.this, PurchasedViewCart.class);
in.putExtra("item_position", var_Pposition); // Cannot resolve method 'putExtra(java.lang.String, java.util.List<java.lang.Integer>

Pass to intent as follows:

Intent intent = new Intent(merchandise.this, PurchasedViewCart.class);  
intent.putIntegerArrayListExtra("myList", (ArrayList<Integer>) var_Pposition );

Retrieve data as follows:

ArrayList<Integer> test = getIntent().getIntegerArrayListExtra("myList");

Put to intent

Intent intent = new Intent(merchandise.this, PurchasedViewCart.class);  
intent.putIntegerArrayListExtra("myList", (ArrayList<Integer>) var_Pposition );  

Get from intent

ArrayList<Integer> test = getIntent(). putIntegerArrayListExtra("myList");

You would call the putExtra (String name, Serializable value) of Intent to store, and getSerializableExtra (String name) for retrieval.

Example:

List<Integer> var_Pposition = new ArrayList<>();
intent.putExtra("var_Pposition", var_Pposition);

In the other Activity:

List<Integer> var_Pposition = (ArrayList<Integer>) getIntent().getSerializableExtra("var_Pposition");

try this,

   static ArrayList<Integer> var_Pposition = new ArrayList<>()

Intent doesn't have putExtra with List parameter. you have to do like,

   intent.putIntegerArrayListExtra("myList", var_Pposition);

or you need to cast it

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM