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

2
  • You have to use intent.putStringArrayListExtra("key",commandList); and get it with getStringArrayListExtra("key"); Commented Jan 20, 2016 at 13:41
  • Or you can modify this answer by converting String[] to arrayList.....stackoverflow.com/questions/6355787/… Commented Jan 20, 2016 at 14:01

2 Answers 2

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");
Sign up to request clarification or add additional context in comments.

3 Comments

I tried this, but it only allows me to put List<String> not List<String[]>
Can show the sample data of commandList?
See the updated answer.
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).

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.