My goal is to permanently add an item to an existing AlertDialog.
The XML array for the AlertDialog is:
<array name="serverchoice">
<item>@string/chicago_server</item>
<item>@string/london_server</item>
<item>@string/sanjose_server</item>
<item>@string/washington_server</item>
<item>@string/chicagoq_server</item>
<item>@string/londonq_server</item>
<item>@string/sanjoseq_server</item>
<item>@string/washingtonq_server</item>
</array>
As you can see it's a list of servers, I'd like a user to be able to add their own server rather than having to use the preset servers.
I have created a page with a text box and a button so a user can enter a server. When the user clicks the Add Server button I'd like the entry to be added to the list.
Currently, when the user chooses a menu item from the AlertDialog it gets processed like this:
// Choose Server method
private void openServerDialog() {
new AlertDialog.Builder(this)
.setTitle(R.string.server_title)
.setItems(R.array.serverchoice,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialoginterface,
int i) {
setServer(i);
}
})
.show();
}
private void setServer(int i) {
if (String.valueOf(i).equals("0")){
CustomServer.setText("mcsord.visualware.com");
}
else if (String.valueOf(i).equals("1")){
CustomServer.setText("mcslhr.visualware.com");
}
else if (String.valueOf(i).equals("2")){
CustomServer.setText("mcssjc.visualware.com");
}
else if (String.valueOf(i).equals("3")){
CustomServer.setText("mcsiad.visualware.com");
}
else if (String.valueOf(i).equals("4")){
CustomServer.setText("qualitytestord.visualware.com");
}
else if (String.valueOf(i).equals("5")){
CustomServer.setText("qualitytestlhr.visualware.com");
}
else if (String.valueOf(i).equals("6")){
CustomServer.setText("qualitytestsjc.visualware.com");
}
else if (String.valueOf(i).equals("7")){
CustomServer.setText("qualitytestiad.visualware.com");
}
}
All I want to do right now is get another permanent item when the user chooses to add one. My last question regarding this wasn't clear enough. I don't want an options menu or context menu.
Thanks
EDIT:
Having looked around even more I can see people are saying it's not possible to dynamically add to an array list.
Currently I have an optionsmenu that presents itself when the menu button is pressed on the android phone itself. The code I use for that is:
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.settings_menu, menu);
return true;
}
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.add_server:
addNewServer();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
Is it possible to create ANOTHER options menu and use that instead of the AlertDialog shown above? Or will I run into the same issue that a list item can't be added dynamically.
I DO HAVE the ability for a user to enter their own URL but it doesn't save it, they'd have to keep typing it in each time.
Thanks