永久增加一个项目到现有alertdialog? [英] Permanently adding an item to an existing alertdialog?

查看:101
本文介绍了永久增加一个项目到现有alertdialog?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的目标是将项目永久添加到现有的AlertDialog。

My goal is to permanently add an item to an existing AlertDialog.

中的XML阵列的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>

正如你可以看到它的服务器列表,我想用户可以添加自己的服务器,而不必使用preSET服务器。

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.

目前,当用户从AlertDialog选择一个菜单项它被这样处理:

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.

感谢

编辑:

说完看了看四周更是我能看到的人都说这是不可能的动态添加到一个数组列表。

Having looked around even more I can see people are saying it's not possible to dynamically add to an array list.

目前我有presents本身时,菜单按钮pssed在Android手机本身$ P $的optionsmenu。在code我用的是:

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);
        }
    }

是否有可能创造另一个选项菜单和使用,而不是AlertDialog上面显示?或者将我碰到了一个列表项不能动态地添加了同样的问题。

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.

我必须输入自己的URL的能力的用户,但它不保存它,他们不得不继续键入它在每个时间。

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.

感谢

推荐答案

我也不知道如果我理解你的问题......但我会尽力回答。 首先,你可以添加dinamically新选项的alertDialog后,你夸大他们。什么,你不能做的是增加新的生产线,从那里你夸大它们的XML。

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:

存储服务器中的共享preferences文件:

Store the servers in a shared preferences file:

保存例如

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

读例如:

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

您还可以使用数据库来存储值。如果你希望有上百台服务器的列表,为previous解决方案的性能会倒在这种情况下,这将是更好的。

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.

最后,你可以存储在一个文件中的信息,但这需要更多的code和我没有看到任何真正的好处。

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.

这篇关于永久增加一个项目到现有alertdialog?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆