允许用户在Android中向菜单添加项目?
我正在尝试为用户添加将项目添加到上下文菜单的功能 菜单的XML数组当前为:允许用户在Android中向菜单添加项目?,android,xml,contextmenu,add,Android,Xml,Contextmenu,Add,我正在尝试为用户添加将项目添加到上下文菜单的功能 菜单的XML数组当前为: <array name="serverchoice"> <item>@string/chicago_server</item> <item>@string/london_server</item> <item>@string/sanjose_server</item> <item>@string
<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>
因此,我的下一个问题是如何处理新条目
无论哪种方式,第一步都是将新条目添加到列表中
任何帮助都会很好
谢谢创建一个包含2个编辑文本的alertdialog;一个用于名称,另一个用于URL;然后用它来引入一个新条目;如中所述,您将无法将用户的输入存储到保存静态创建的服务器的阵列资源中。您必须将用户输入的服务器存储到您保存到手机内存、SQLite数据库或使用共享首选项(推荐)的文件中。[http://developer.android.com/guide/topics/data/data-storage.html] 在回答你的第二个问题时,我可能采用了不同的总体方法 在应用程序的入口点中,使用共享首选项设置系统
// Shared Preference string
public static final String PREFS = "SomeName";
// Editor to customize preferences
private Editor settingsEditor;
// Shared preference
private SharedPreferences prefs;
//list that will hold all the servers for the menu
private ArrayList<String> menuList = new ArrayList<String>();
之后,您将需要填充菜单列表
int i = prefs.getInt("menuItemCount", 0);
for(j = 0; j < i; j++)
menuList.add(prefs.getString("sever" + j,"");
希望这有帮助。我想会有帮助的you@manojKumar所有这些示例似乎都是为了创建选项菜单,我使用的是alertdialog而不是选项菜单。还有别的地方我应该找吗?谢谢你提供的信息。我希望输入的服务器始终保留在列表中,而不仅仅是一次性输入。这可能吗?
prefs = getSharedPreferences(PREFS, MODE_PRIVATE);
// Get shared preferences and set up the preference editor
settingsEditor = prefs.edit();
// See if the app has been initialized
if (!prefs.getString("initialized", "").equals("yes")) {
settingsEditor.putString("server0","mcsord.visualware.com");
settingsEditor.putString("server1","mcslhr.visualware.com");
//...do the above step to put the rest of them in
settingsEditor.putInt("menuItemsCount",8);
settingsEditor.putBoolean("initialized", true);
settingsEditor.commit();
}
int i = prefs.getInt("menuItemCount", 0);
for(j = 0; j < i; j++)
menuList.add(prefs.getString("sever" + j,"");
private void openServerDialog() {
new AlertDialog.Buildr(this)
.setTitle(R.string.server_title)
.setItems(R.array.serverchoice,
new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialoginterface,
int i) {
CustomServer.setText(menuList.get(i));
}
}).show();
}