3

I have to get a part of my content provider query in a String[] format. Currently I use:

        String[] selectionArgs = {"",""};
    selectionArgs[0] = Integer.toString(routeScheduleID);
    selectionArgs[1] = runDate.toString();

But in this case I have an unknown number of elements.

How can I change the number at runtime (or use something like an Array and convert back to String[]. Is this possible?

2
  • 7
    ...Use a List<String>? Commented Jun 27, 2012 at 11:14
  • Yes, it's possible to convert between lists and arrays; check Arrays.asList and the List API. Commented Jun 27, 2012 at 11:16

5 Answers 5

3

You can use List<String> to get your data and then get the array out of it:

List<String> lst = new ArrayList<String>();
lst.add(Integer.toString(routeScheduleID);
lst.add(runDate.toString());
lst.add(...);
...

String[] selectionArgs = lst.toArray(new String[lst.size()]);
Sign up to request clarification or add additional context in comments.

Comments

2

You can use List for this. A List of Strings like this - List<String> str = new ArrayList<String>();

Comments

1

You can use a list to populate the data then convert the list into an array:

List<String> list = new ArrayList<String>();
list.add(item1);
list.add(item2);
...

String[] array = list.toArray(new String[0]);

Comments

1
List<String> selectionArgs = new ArrayList<String>();

selectionArgs.add(Integer.toString(routeScheduleID));
selectionArgs.add(runDate.toString());
selectionArgs.add(...).
 ...........

String[] array= selectionArgs.toArray(new String[selectionArgs.size()]);

Comments

1
List<String> selectionArgsList = new ArrayList<String>();
selectionArgsList.add("string1");
selectionArgsList.add("string2");

String[] selectionArgs = new String[selectionArgsList.length];
selectionArgsList.toArray(selectionArgs);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.