Reputation: 244
I'm trying to build an AlertDialog using Builder. My dialog has to have multiple option the user can choose from. I found on android developers website that I can use builder.setItems(int, DialogInterface.onClickListener). my problem is that im trying to pass a List addresses instead of the int. I want the user to choose an option from the list of addresses. Here's my code for illustration:
private List<Address> addresses;
protected void updateMap() {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Pick an Address");
builder.setItems(addresses, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface arg0, int choosenAddress) {
//address = addresses.get(choosenAddress); //choose result from the array
}
});
In the builder.setItems ..i get an error saying this argument does not take list.
Upvotes: 1
Views: 5819
Reputation: 13807
An extension to nandeesh's answer
String [] addressStrings = new String [addresses.size()];
for(int i=0; i<addresses.size(); i++)
addressStrings[i] = addresses.get(i).toString();
You need to override the toString()
method of the Address
class, to create meaningful String
representations of Address
objects.
Now in the onClick()
method you can still access the Address
object by the given index, because the size, and element order of the addresses
list and the addressStrings
array are the same.
Address selected = addresses.get(choosenAddress);
Upvotes: 1
Reputation: 24820
SetItems
takes Charsequence Array as a parameter, so you will have to convert List<Address>
to List<String>
and then use list.toarray()
Upvotes: 2