-1

I have this list of arrays that I need for my service to run

private List<String[]> commandList = new ArrayList<>();

Sample Data:

{"jump", "100", "100"}
{"walk", "100", "100"}
{"jump", "100", "100"}

I tried passing it through putExtra

Intent serviceIntent = new Intent(macroService.class.getName());
serviceIntent.putExtra("CommandList", commandList);
context.startService(serviceIntent);

however the putExtra method only allows me to input strings and string arrays. Is there any way to pass a list of string arrays?

LuxuryWaffles
  • 1,518
  • 4
  • 27
  • 50
  • You have to use intent.putStringArrayListExtra("key",commandList); and get it with getStringArrayListExtra("key"); – Opiatefuchs Jan 20 '16 at 13:41
  • Or you can modify this answer by converting String[] to arrayList.....http://stackoverflow.com/questions/6355787/how-to-pass-arraylisthashmapstring-stringfrom-one-activity-to-another – Opiatefuchs Jan 20 '16 at 14:01

2 Answers2

2

Use putStringArrayListExtra method instead of putExtra.

serviceIntent.putStringArrayListExtra("CommandList", commandList);

Also you can add your list in a HashMap and than pass this HashMap to next activity like below:

 HashMap<String, List> tmp = new HashMap<>();
 tmp.put("data", commandList);
 serviceIntent.putExtra("tmpData", tmp);

And to retrieve this data in the destination activity, you can do this:

 HashMap<String, List> tmp1 = (HashMap<String, List>) getIntent().getExtras().get("tmpData");
 List<String[]> testd = tmp1.get("data");
Prerak Sola
  • 9,517
  • 7
  • 36
  • 67
0

From what I can see there is no function that does what you want, but, depending on how you are building and using the list, you could instead use a Bundle to collect your arrays and then add them to the intent with putExtras(Bundle extras).

gabga
  • 178
  • 1
  • 10