开发者

Add data to CharSequence dynamically [duplicate]

This question already has answers here: 开发者_如何学编程 Android, how to populate a CharSequence array dynamically (not initializing?) (6 answers) Closed 10 years ago.

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!


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();
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜