1

I have the following code to display a list of clickable items (user can choose more than one item)

 return new AlertDialog.Builder(this)
        .setTitle("Users")          
        .setMultiChoiceItems(csUser, _selections,
                new DialogSelectionClickHandler())
        .setPositiveButton("OK", new DialogButtonClickHandler())
        .create();

It works fine using a CharSequence[] csUser = {"User1", "User2", "User3"}......

But I want to fill it with a list of users within a Database,

Is there a way to dynamically add items to a CharSequence[] ?

Anyone kwows another alternative for this?

Thank you!

skaffman
  • 398,947
  • 96
  • 818
  • 769
JPBoucher
  • 49
  • 1
  • 4

1 Answers1

0

Why are you using an array? If you don't know the number of the users added to the list, maybe you should use a LinkedList. And you create a method to the builder that adds each item:

class Builder {

    private List<CharSequence> users = new LinkedList<CharSequence>();

    ...

    public Builder addUser(CharSequence user) {
        users.add(user);
        return this;
    }
}

Then in your create() method, you transform the LinkedList in what is needed for the AlertDialog.

The "client" code would look like this:

    Builder builder = new AlertDialog.Builder(this)
        .setTitle("Users")
        .setPositiveButton("OK", new DialogButtonClickHandler());

    for (...) {
        CharSequence user = ...
        builder.addUser(user);
    }

    return builder.setSelections(selections)
        .setHandler(new DialogSelectionClickHandler())
        .create();
Nicolae Albu
  • 1,235
  • 6
  • 6