Skip to main content

APIzation: Replication Package

[Q#7063831][A#7063917] Android, how to populate a CharSequence array dynamically (not initializing?)

https://stackoverflow.com/q/7063831

How do I change something like this: to:

Answer

https://stackoverflow.com/a/7063917

Use a List object to manage items and when you have all the elements then convert to a CharSequence. Something like this:

APIzation

List<String> listItems = new ArrayList<String>();

listItems.add("Item1");
listItems.add("Item2");
listItems.add("Item3");

final CharSequence[] charSequenceItems = listItems.toArray(new CharSequence[listItems.size()]);
package com.stackoverflow.api;

import java.util.ArrayList;
import java.util.List;

public class Human7063917 {

  public static CharSequence[] getCharSequence(String... items) {
    List<String> listItems = new ArrayList<>();
    for (String item : items) {
      listItems.add(item);
    }

    return listItems.toArray(new CharSequence[listItems.size()]);
  }
}

package com.stackoverflow.api;

import java.util.ArrayList;
import java.util.List;

/**
 * Android, how to populate a CharSequence array dynamically (not initializing?)
 *
 * @author APIzator
 * @see <a href="https://stackoverflow.com/a/7063917">https://stackoverflow.com/a/7063917</a>
 */
public class APIzator7063917 {

  public static CharSequence[] initialize(List<String> listItems)
    throws Exception {
    return listItems.toArray(new CharSequence[listItems.size()]);
  }
}