Add data to CharSequence dynamically [duplicate]
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();
精彩评论