I am trying to add the ability for a user to add an item to a context menu.
The XML array for the menu is currently:
<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 clicked the Add Server button I’d like the entry to be added to the list.
The way I’m currently processing the menu items when clicked is below:
// 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");
}
}
So my next question is how would I then process the new entry.
Either way the first step is getting the new entry added to the list.
Any help would be great.
Thanks
You wouldn’t be able to store the user’s input into the array resource that holds the servers you statically created. You would have to store the server the user entered into a file that you save into the phone’s memory, a SQLite database or using the Share Preferences (recommended). [http://developer.android.com/guide/topics/data/data-storage.html].
In answer to your second question, I may have gone with a different overall approach.
In your entry point for you application set up your system using the shared preferences
in your onCreate method (the very first time the user opens the application) you’re going to put your defined server names into the system
Afterward, you’re going to want to populate your menu list
And now what you do is after the user opens the page, types in their server name and clicks the button:
If it’s a dialog box that’s in the same activity you can add it to the list and the the shared preferences
If it’s in another activity, send it through and intent, override the onActivityResult method and then add the string to the list and the shared preferences.
In your onCreateContextMenu method, loop through the list that contains all the servers and add them to your menu. Lastly you could do this in your code
Hope this helps.