Permanently adding an item to an existing alertdia

2019-03-01 01:40发布

问题:

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

回答1:

I'm nor sure if I understood your question... but I'll try to answer. First, you can add dinamically new options to the alertDialog after you inflate them. What you can't do is add new lines to the xml from where you inflate them.

Having said that, you need to store the new servers somewhere when the activity finish, so you can recover them later. For this you have several options, and I'll start with the one I believe is the simplest:

Store the servers in a shared preferences file:

Saving example

    Editor editor = getSharedPreferences("FileName", MODE_PRIVATE).edit();
    editor.clear();
    editor.putString("server1", "serverName1");
    editor.putString("server2", "serverName2");
    editor.commit();

Reading example:

    SharedPreferences preferences = getSharedPreferences("FileName", MODE_PRIVATE);
    preferences.getString("server1", "defaultValue");
    preferences.getString("server2", "defaultValue");

You can also use a database to store the values. This would be better if you expect to have a list with hundreds of servers, as the performance of previous solution would be pour in this case.

Finally, you could store the information in a file, but that would require more code and I don't see any true benefit of it.

If I didn't answer you question, just let me know. Good luck.



回答2:

You can leave the defaults in the xml file but you'll need to store the custom servers in a database. Then you can pull from both sources into a single array and put that in the dialog.

final CharSequence[] items = {"Red", "Green", "Blue"};

AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Pick a color");
builder.setItems(items, new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int item) {
        Toast.makeText(getApplicationContext(), items[item], Toast.LENGTH_SHORT).show();
    }
});
AlertDialog alert = builder.create();